2017-02-20 67 views
1

我正在嘗試使用Wunderground APIPHP向用戶輸出其給定城市降水(POP)的概率。 Wunderground API以JSON格式返回其請求。然而,我目前沒有收回任何東西,我不知道問題是什麼。使用Wunderground API獲取城市的平均降水量+ PHP

下面是我的代碼:

<?php 
    $json_string = file_get_contents("http://api.wunderground.com/api/API_KEY/forecast/q/IA/Cedar_Rapids.json"); 
    $parsed_json = json_decode($json_string); 
    $location = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}; 
    $pop = $parsed_json->{'pop'}; 
    echo "Percipation: ${pop}%"; 
?> 

我知道這個任務是accomplishable使用Javascript,我最終還是希望它接口到MySQL數據庫。

***我不包括我的API密鑰,因爲我每天的API調用的數量有限,在那之後我開始得到所收取的費用:(

編輯1:

下面是HTTP響應頭

array(13) { 
    [0]=> string(15) "HTTP/1.0 200 OK" 
    [1]=> string(20) "Accept-Ranges: bytes" 
    [2]=> string(29) "Cache-Control: max-age=604800" 
    [3]=> string(23) "Content-Type: text/html" 
    [4]=> string(35) "Date: Mon, 20 Feb 2017 20:55:54 GMT" 
    [5]=> string(17) "Etag: "359670651"" 
    [6]=> string(38) "Expires: Mon, 27 Feb 2017 20:55:54 GMT" 
    [7]=> string(44) "Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT" 
    [8]=> string(22) "Server: ECS (cpm/F9D5)" 
    [9]=> string(21) "Vary: Accept-Encoding" 
    [10]=> string(12) "X-Cache: HIT" 
    [11]=> string(20) "Content-Length: 1270" 
    [12]=> string(17) "Connection: close" 
} 

編輯2:

下面是JSON返回(打起精神來)

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "forecast": 1 
    } 
    } 
     , 
    "forecast":{ 
     "txt_forecast": { 
     "date":"1:04 PM CST", 
     "forecastday": [ 
     { 
     "period":0, 
     "icon":"rain", 
     "icon_url":"http://icons.wxug.com/i/c/k/rain.gif", 
     "title":"Monday", 
     "fcttext":"Cloudy with periods of rain. Thunder possible. High 61F. Winds S at 10 to 15 mph. Chance of rain 90%.", 
     "fcttext_metric":"Rain. Thunder possible. High 16C. Winds S at 15 to 25 km/h. Chance of rain 90%.", 
     "pop":"90" 
     } 
     , 
     { 
     "period":1, 
     "icon":"nt_chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_chancerain.gif", 
     "title":"Monday Night", 
     "fcttext":"Rain showers this evening with overcast skies overnight. Thunder possible. Low 39F. Winds W at 5 to 10 mph. Chance of rain 60%.", 
     "fcttext_metric":"Showers early, then cloudy overnight. Thunder possible. Low 4C. Winds WSW at 10 to 15 km/h. Chance of rain 60%.", 
     "pop":"60" 
     } 
     , 
     { 
     "period":2, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Tuesday", 
     "fcttext":"Some clouds in the morning will give way to mainly sunny skies for the afternoon. Near record high temperatures. High around 65F. Winds WSW at 5 to 10 mph.", 
     "fcttext_metric":"Some clouds in the morning will give way to mainly sunny skies for the afternoon. Near record high temperatures. High 18C. Winds WSW at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":3, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Tuesday Night", 
     "fcttext":"Clear skies. Low 39F. Winds SSW at 5 to 10 mph.", 
     "fcttext_metric":"Clear skies. Low 4C. Winds SSW at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":4, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Wednesday", 
     "fcttext":"Mostly sunny skies with gusty winds developing during the afternoon. High 71F. Winds SSW at 20 to 30 mph.", 
     "fcttext_metric":"Mostly sunny skies. Becoming windy during the afternoon. High 21C. Winds SSW at 30 to 50 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":5, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Wednesday Night", 
     "fcttext":"A mostly clear sky. Low 38F. Winds WNW at 5 to 10 mph.", 
     "fcttext_metric":"A mostly clear sky. Low 3C. Winds WNW at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":6, 
     "icon":"chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif", 
     "title":"Thursday", 
     "fcttext":"Partly cloudy in the morning. Increasing clouds with periods of showers later in the day. High 54F. Winds NE at 15 to 25 mph. Chance of rain 80%.", 
     "fcttext_metric":"Partly cloudy skies during the morning hours will give way to occasional showers in the afternoon. High 12C. Winds NE at 25 to 40 km/h. Chance of rain 90%.", 
     "pop":"80" 
     } 
     , 
     { 
     "period":7, 
     "icon":"nt_tstorms", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_tstorms.gif", 
     "title":"Thursday Night", 
     "fcttext":"Showers along with a few thunderstorms. Low 39F. Winds ENE at 20 to 30 mph. Chance of rain 100%.", 
     "fcttext_metric":"Windy with showers and thunderstorms likely. Low 4C. Winds ENE at 30 to 50 km/h. Chance of rain 100%.", 
     "pop":"100" 
     } 
     ] 
     }, 
     "simpleforecast": { 
     "forecastday": [ 
     {"date":{ 
    "epoch":"1487638800", 
    "pretty":"7:00 PM CST on February 20, 2017", 
    "day":20, 
    "month":2, 
    "year":2017, 
    "yday":50, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"0", 
    "monthname":"February", 
    "monthname_short":"Feb", 
    "weekday_short":"Mon", 
    "weekday":"Monday", 
    "ampm":"PM", 
    "tz_short":"CST", 
    "tz_long":"America/Chicago" 
}, 
     "period":1, 
     "high": { 
     "fahrenheit":"61", 
     "celsius":"16" 
     }, 
     "low": { 
     "fahrenheit":"39", 
     "celsius":"4" 
     }, 
     "conditions":"Rain", 
     "icon":"rain", 
     "icon_url":"http://icons.wxug.com/i/c/k/rain.gif", 
     "skyicon":"", 
     "pop":90, 
     "qpf_allday": { 
     "in": 0.28, 
     "mm": 7 
     }, 
     "qpf_day": { 
     "in": 0.27, 
     "mm": 7 
     }, 
     "qpf_night": { 
     "in": 0.08, 
     "mm": 2 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 15, 
     "kph": 24, 
     "dir": "S", 
     "degrees": 169 
     }, 
     "avewind": { 
     "mph": 12, 
     "kph": 19, 
     "dir": "S", 
     "degrees": 169 
     }, 
     "avehumidity": 84, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1487725200", 
    "pretty":"7:00 PM CST on February 21, 2017", 
    "day":21, 
    "month":2, 
    "year":2017, 
    "yday":51, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"0", 
    "monthname":"February", 
    "monthname_short":"Feb", 
    "weekday_short":"Tue", 
    "weekday":"Tuesday", 
    "ampm":"PM", 
    "tz_short":"CST", 
    "tz_long":"America/Chicago" 
}, 
     "period":2, 
     "high": { 
     "fahrenheit":"65", 
     "celsius":"18" 
     }, 
     "low": { 
     "fahrenheit":"39", 
     "celsius":"4" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":10, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "WSW", 
     "degrees": 238 
     }, 
     "avewind": { 
     "mph": 9, 
     "kph": 14, 
     "dir": "WSW", 
     "degrees": 238 
     }, 
     "avehumidity": 63, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1487811600", 
    "pretty":"7:00 PM CST on February 22, 2017", 
    "day":22, 
    "month":2, 
    "year":2017, 
    "yday":52, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"0", 
    "monthname":"February", 
    "monthname_short":"Feb", 
    "weekday_short":"Wed", 
    "weekday":"Wednesday", 
    "ampm":"PM", 
    "tz_short":"CST", 
    "tz_long":"America/Chicago" 
}, 
     "period":3, 
     "high": { 
     "fahrenheit":"71", 
     "celsius":"22" 
     }, 
     "low": { 
     "fahrenheit":"38", 
     "celsius":"3" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":10, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 30, 
     "kph": 48, 
     "dir": "SSW", 
     "degrees": 202 
     }, 
     "avewind": { 
     "mph": 21, 
     "kph": 34, 
     "dir": "SSW", 
     "degrees": 202 
     }, 
     "avehumidity": 57, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1487898000", 
    "pretty":"7:00 PM CST on February 23, 2017", 
    "day":23, 
    "month":2, 
    "year":2017, 
    "yday":53, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"0", 
    "monthname":"February", 
    "monthname_short":"Feb", 
    "weekday_short":"Thu", 
    "weekday":"Thursday", 
    "ampm":"PM", 
    "tz_short":"CST", 
    "tz_long":"America/Chicago" 
}, 
     "period":4, 
     "high": { 
     "fahrenheit":"54", 
     "celsius":"12" 
     }, 
     "low": { 
     "fahrenheit":"39", 
     "celsius":"4" 
     }, 
     "conditions":"Chance of Rain", 
     "icon":"chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif", 
     "skyicon":"", 
     "pop":80, 
     "qpf_allday": { 
     "in": 0.70, 
     "mm": 18 
     }, 
     "qpf_day": { 
     "in": 0.07, 
     "mm": 2 
     }, 
     "qpf_night": { 
     "in": 0.64, 
     "mm": 16 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 25, 
     "kph": 40, 
     "dir": "NE", 
     "degrees": 42 
     }, 
     "avewind": { 
     "mph": 19, 
     "kph": 31, 
     "dir": "NE", 
     "degrees": 42 
     }, 
     "avehumidity": 72, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     ] 
     } 
    } 
} 
+0

請添加服務器的響應,包括HTTP標頭。看看這裏http://php.net/manual/en/reserved.variables.httpresponseheader.php尋求幫助。 – 0xJoKe

+0

@ 0xJoKe - 它被添加了...你想看看嗎? – EDCisBack

+0

謝謝。你也可以添加'$ json_string'的內容給你提問嗎?您似乎收到1270字節的內容。你有什麼例外嗎? – 0xJoKe

回答

1

我想你使用了錯誤的變量

$pop = $parsed_json->{'pop'}; 

$pop = $location[0]->{'pop'}; 

試試看注意$location應該是一個數組,所以嘗試直接訪問它($location->{'pop'})將無法正常工作。 0索引僅作爲示例。