加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用google api获取两个位置之间的距离

发布时间:2020-12-13 17:22:32 所属栏目:PHP教程 来源:网络整理
导读:我有以下代码: $output = file_get_contents("http://maps.google.com/maps/nav?q=from:39.519894,-5.105667%20to:51.129079,1.306925");$distance = json_decode($output); 我从Google API获得JSON响应,但是当我使用json_decode()解码JSON时,我得到一个NUL
我有以下代码:

$output = file_get_contents("http://maps.google.com/maps/nav?q=from:39.519894,-5.105667%20to:51.129079,1.306925");

$distance = json_decode($output);

我从Google API获得JSON响应,但是当我使用json_decode()解码JSON时,我得到一个NULL值意味着该函数无法对其进行解码.

这背后的原因是什么?

解决方法

返回值的格式错误的UTF-8字符可能是因为编码不正确.你需要的只是utf8_encode

$json = file_get_contents('http://maps.google.com/maps/nav?q=from:39.519894,1.306925');
$data = json_decode(utf8_encode($json),true);
var_dump($data);

See Live Demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读