json — получить среднее количество осадков в городе с помощью Wunderground API + переполнение стека

В настоящее время я пытаюсь вывести пользователю вероятность осадков (POP) в их данном городе, используя Wunderground API а также PHP, 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 (приготовьтесь)

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

1

Решение

Я думаю, что вы используете неправильную переменную в

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

Попробуйте это с

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

Быть в курсе, что $location должен быть массив, поэтому пытаюсь получить к нему прямой доступ ($location->{'pop'}) не будет работать. 0 Индекс служит только в качестве примера.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector