我可能花了一整天的时间试图弄清楚这一点。我在stack上读过多个问题,也读过文章和查看过文档,但我似乎不明白为什么这批代码只会产生null输出。我是不是漏掉了括号,说错话了,等等?
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
refresh_requency => 30
);
echo json_encode($array);
?>
我再次问的是,有人能不能向我指出或告诉我我做错了什么。是我的服务器没有正确处理数据吗?这可能是一种可能性。
我尝试过的另一件事是只打印出$temp
和/或其他类似$str
的变量。当我这样做的时候,他们甚至没有出现,所以我认为我的问题是不确定如何解决
更新
我已经得出结论,这是我的网络托管服务。就像我加上var_dump($json)
一样,我得到一个空输出null
输出。
此外,为了确认它是我的网络主机,如果我运行error_reporting(E_ALL); ini_set('display_errors', 1);
,它会指向不允许传出连接的文件php.ini。我在本地家庭服务器上编辑了同一个文件(树莓派),运行了同一文件,效果很好。
以下是上述代码的工作解决方案:
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
?>
当我执行你的代码时,我在你的代码片段中发现了2个问题:
1) 您正试图使用stdClass类型的对象作为数组。
解决方案:
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp = $json->main->temp_min;
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
?>
2) 您没有将数组键置于引号中:
$array = array(
"content" => $content,
refresh_requency => 30
);
应该是:
$array = array(
"content" => $content,
"refresh_requency" => 30
);
像这个一样访问$temp
$temp = $json->main->temp_min;
您将获得所需的输出。
此外,您需要在php.ini
配置文件中允许allow_url_fopen
。一些主机出于安全原因不允许使用
$json = json_decode($str, true);
您需要第二个参数来将json字符串转换为关联数组,而不是对象。您正在尝试使用数组($json['main']['temp_min']
),而不是对象。还有
$array = array(
"content" => $content,
"refresh_requency" => 30
);
代码看起来像
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$content = $json['main']['temp_min'];
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
结果是http://codepad.viper-7.com/euBNAk:
{"content":44.6,"refresh_requency":30}
json_decode()的第二个参数是assoc(associative)。默认情况下为0。当它为0(默认值)时,json_decode()将返回一个对象,而不是数组。这就是为什么您无法使用$json['main']['temp_min']访问temp_min;
但是,如果将值1用作第二个参数,则函数将返回一个数组。参数1表示与1关联的设置(true)。因此,使用$json=json_decode($str,true);而不是$json=json_decode($str);。您将能够使用$json['main']['temp_min']进行访问;现在
此外,您忘记了第13行的双引号(refresh_requency)。祝你好运。
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
只想帮助
我这样修改你的代码,那是正确的
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, TRUE); // Wrong here
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30 // And wrong here, it's must string
);
echo json_encode($array);
?>
有关php中json解码到数组或对象的更多信息http://php.net/manual/en/function.json-decode.php