我目前正在尝试使用Wunderground API和PHP
向用户输出其指定城市中的降水概率(POP)。 Wunderground API以JSON格式返回其请求。然而,我目前没有得到任何回报,我不知道问题是什么。
以下是我的代码:
<?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}%";
?>
我知道这个任务可以用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回复(Brace self)
{
"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 :(得分:1)
我认为你在
中使用了错误的变量$pop = $parsed_json->{'pop'};
尝试
$pop = $location[0]->{'pop'};
请注意$location
应该是一个数组,因此尝试直接访问它($location->{'pop'}
)将无效。 0
索引仅作为示例。