Wunderground PHP

1234

我仅了解了一些有关JSON的知识,并且目前正在尝试使用天气地下API和PHP来显示温度和天气状况,但显示的是温度,但没有显示天气状态。这是我的代码:

<?php
$json_string = file_get_contents("http://api.wunderground.com/api/9fca46f2c0517556/geolookup/conditions/q/UK/Leeds.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$weather =$parsed_json->{'weather'};
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
echo "Current temperature in ${location} is: ${temp_c}\n degrees and it is currently ${weather}";
?>
拉斐尔·维吉(RaphaelVigée)

您在$parsed_json对象访问中忘记了一个节点,请替换:

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

和 :

$weather = $parsed_json->current_observation->weather;

以及更一般的用法:

$json_string = file_get_contents("http://api.wunderground.com/api/9fca46f2c0517556/geolookup/conditions/q/UK/Leeds.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->location->city;
$weather =$parsed_json->current_observation->weather;
$temp_c = $parsed_json->current_observation->temp_c;
echo "Current temperature in ${location} is: ${temp_c}\n degrees and it is currently ${weather}";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章