2017-02-20 3 views
1

현재 Wunderground APIPHP을 사용하여 주어진 도시에서 강수량 (POP) 확률을 사용자에게 출력하려고합니다. Wunderground API는 요청을 JSON 형식으로 리턴합니다. 그러나 나는 현재 아무것도 얻고 있지 않으며 나는 그 문제가 무엇인지 모른다. 나는 결국은 MySQL 데이터베이스에 인터페이스 할, 나는이 작업은 자바 스크립트와 accomplishable 것을 알고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 키를 포함하지 않았다

편집 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으로 복귀 (이다 나는 당신이 그렇게 노력 $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 색인은 예제로만 제공됩니다.