2016-11-28 6 views
0

좋아, 오픈 날씨 API를 사용하는 방법을 이해하는 데 어려움을 겪고 있습니다. 어떻게 등 certains시 온도, 습도를받을 수 있나요, 나는PHP OpenWeather

<?php 
    $request = file_get_contents('http://api.openweathermap.org/data/2.5/forecast/city?id=myidblablabla'); //example ID 

    $jsonPHP = json_decode($request); 

    echo $jsonPHP->city; 

?> 

로 사용 시도하지만

Catchable fatal error: Object of class stdClass could not be converted to string in

지금은 물어 봐야 하나 개 더 질문이 말하는 오류가? 받은 코드에서 나는 단지 모스크바 만받습니다.

+0

문서가 귀하에게 중요하지 않았습니까? –

+0

오픈 소스 라이센스로 날씨가 공개 되었습니까? 그것은 좋은 소식입니다! – arkascha

+0

일종의, couldnt는 100 % 정확한 답변을 찾았습니다. 나는 그걸 들여다 보았으나 내가 한 모든 시도는 실패했다. –

답변

1

간단히하기 위해 json을 배열로 변형 할 수 있습니다.

$jsonPHP = json_decode($request,true); 

이제 간략하게 살펴 보겠습니다. 문서 (http://openweathermap.org/current),

참고 또한 에 따르면 나는 전에이 API를 사용한 적이 없으니까. 나는 여기서 도우려고하고있다. 당신이 api.openweathermap.org/data/2.5/weather?lat=35&lon=139

에 충돌하는 경우

{"coord":{"lon":139,"lat":35}, 
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049}, 
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}], 
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04}, 
"wind":{"speed":7.31,"deg":187.002}, 
"rain":{"3h":0}, 
"clouds":{"all":92}, 
"dt":1369824698, 
"id":1851632, 
"name":"Shuzenji", 
"cod":200} 

는 이제 날씨를습도을 원하는 가정

그것은 응답, 그것은 단지 :

날씨 :

echo $jsonPHP["weather"][0]["id"]; 

습도 : 당신이 경우에 대한

{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."} 

http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139 및 얻을 응답을 치면

echo $jsonPHP["main"]["humidity"]; 

참고도 즉, 그들은 여기에서 설명했습니다

http://openweathermap.org/faq#error401 즉 :

Q: API calls return an error 401

A: Starting from 9 October 2015 our API requires a valid APPID for access. Note that this does not mean that our API is subscription-only now - please take a minute to register a free account to receive a key.

We are sorry for inconvenience but this is a necessary measure that will help us deliver our services to you faster and more reliably.

For FOSS developers: we welcome free and open source software and are willing to help you. If you want to use OWM data in your free software application please register an API key and file a ticket describing your application and API key registered. OWM will review your request lift access limits for your key if used in open source application.

+0

네,하지만 $ jsonPHP를 사용할 때 [ "weather"] [0] [ "id"]; 나는 정의되지 않은 인덱스를 얻는다 : 날씨 –

+1

당신은 내가 * APPID *를 가지고 있지 않기 때문에 내가 http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139에 접근 할 수 없다는 것을 잊는다. 그래서 나는 시뮬레이션을 만들었다. 응답은 * json * 파일로 저장하고 올바른 출력을 얻습니다. 나는 804를 얻는다. 그것은 맞다! –

+0

네,하지만 이건 var_dump에서 실제로 얻은 것입니다. http://pastebin.com/KteUqtqc 나는 [list] [0] [ 'weather']에 접근하려고했는데, 그걸 작동시킬 수 없습니다. string –