-1
{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
PHP:
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
var_dump(json_decode($response));
echo $response->location[0]->name;
API調用:http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Tirana
如何捕獲'json_decode($ response'到本地變量,然後解析JSON? – karthikr
地點是一維陣列,而不是一個多維數組。所以刪除'[0]' - >'echo $ response-> location-> name;' – Sean
只是一個想法:即使基本服務是免費的,你也不應該提供包含有效API密鑰的鏈接,但是你應該將該密鑰交換爲新的(並且,由於密鑰嵌入在URL中,所以將來應該使用HTTPS) –