【发布时间】:2020-01-18 22:20:17
【问题描述】:
$data = file_get_contents($url);
$result = json_decode($data, true);
使用上面的代码,我想将下面的 url 输出捕获到 $data,但是 file_get_contents 没有给 $data 任何东西。请帮助我解决这个问题,也请让我知道 PHP 中是否有其他方法可以做到这一点。
{
"destination_addresses" : [ "Boyces Rd, Wisbech PE13 2JT, UK" ],
"origin_addresses" : [ "Battersea, London SW8 3QR, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "174 km",
"value" : 173989
},
"duration" : {
"text" : "2 hours 42 mins",
"value" : 9702
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
【问题讨论】:
-
如果
file_get_contents()失败,那么您将收到警告。启用错误报告并检查您的网络服务器日志。 -
检查 allow_url_open 是否设置为 on 或者您可以尝试使用 cURL 获取该内容。这里有一个例子:stackoverflow.com/questions/10354805/…
标签: php file-get-contents