【问题标题】:Can't get JSON data from Google Maps request with cuRL无法使用 cuRL 从 Google 地图请求中获取 JSON 数据
【发布时间】:2013-09-24 13:06:48
【问题描述】:

我正在尝试使用 curl 编写请求。我想得到两点之间的距离。因此,我从我的数据库中获取这些点的数据来构建字符串。当我回显它时,最终的字符串看起来像这样。

http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+Düsseldorf+Königsallee 59&destinations=40215+Düsseldorf&sensor=false

我也试过用 https 代替 http 但结果是一样的。

如您所见,它返回了一个完美的 JSON 响应,但是当我之后执行此操作时,我总是得到一个错误。

public function request($signedUrl) {
        $ch = curl_init($signedUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        $this->response = json_decode($data);
    }

$signedUrl 是 requestUrl。

当我var_dump($data) 时,我从 Google 得到的错误是

400. That’s an error.

Your client has issued a malformed or illegal request. That’s all we know. "

当我 var_dump 响应时,它只会给我 null。

这可能是什么问题,我该如何解决?我也尝试使用file_get_contents 阅读它,但没有成功

【问题讨论】:

  • 网址中Königsallee59之间的空白怎么办?
  • 嗨@Havelock - 尝试用+ 替换空格并尝试完全删除它,但我仍然得到相同的结果。仍然说NULL。这是更改后的requestURL http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+Düsseldorf+Königsallee59&destinations=40215+Düsseldorf&sensor=false

标签: php json google-maps curl


【解决方案1】:

有效 [已测试] 。由于 URL 未编码,因此您收到了错误的请求。

<?php
$urlx='http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+D%C3%BCsseldor%20%E2%80%8Bf+K%C3%B6nigsallee59&destinations=40215+D%C3%BCsseldorf&sensor=false';
$ch = curl_init($urlx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
var_dump(($data));

【讨论】:

  • 哈哈,好的,是的。所以我想只是用 urlencode(url) 包装 url,应该做的工作,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2018-10-03
相关资源
最近更新 更多