2017-02-20 1 views
1

現在、Wunderground APIPHPを使用して、指定都市で降水確率(POP)をユーザーに出力しようとしています。 Wunderground APIはリクエストをJSON形式で返します。しかし、私は現在何も戻っていないし、私は問題が何であるか分からない。私は最終的には、MySQLデータベースへのインターフェイスにしたい、私はこのタスクはJavaScriptを使用して達成可能であることを知っている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}%"; 
?> 

以下は私のコードです。

***私は一日あたりのAPIコールの数が限られている、その後、私は:(お金を請求取得を開始するので、私は私のAPIキーが含まれていませんでした

EDIT 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" 
} 

EDIT 2:以下

JSONリターンが(あります$locationはそうしようとし、配列でなければならないことに注意してください

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

でそれを試してみてください、私はあなたが

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

で間違った変数を使用していると思う

{ 
    "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

)自分自身をブレースそれに直接アクセスする($location->{'pop'})は動作しません。 0インデックスは例としてのみ提供されます。

関連する問題