简体   繁体   English

无法使用cuRL从Google Maps请求中获取JSON数据

[英]Can't get JSON data from Google Maps request with cuRL

I'm trying to write a request with curl. 我正在尝试使用curl编写请求。 I want to get the distance between two points. 我想得到两点之间的距离。 Therefore I'm getting the data of those points from my db to build the string. 因此,我从数据库中获取这些点的数据以构建字符串。 The final string looks like this when I echo it. 当我回显最终字符串时,它看起来像这样。

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

I also tried it with https instead of http but the result was the same. 我也用https而不是http尝试了,但是结果是一样的。

As you can see, it returns a perfectly fine JSON-Response, but when I do this afterwards, I always get an error. 如您所见,它返回了一个非常好的JSON-Response,但是当我随后执行此操作时,总是会收到错误消息。

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);
    }

The $signedUrl is the requestUrl. $signedUrl是requestUrl。

The error from Google I get, when I var_dump($data) is 我从var_dump($data)获取的Google错误是

400. That’s an error.

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

When I var_dump the response, it just gives me null. 当我var_dump响应时,它只是给我null。

What could be the problem here and how could I fix it? 这里可能是什么问题,我该如何解决? I also tried to read it in with file_get_contents but without success 我也尝试用file_get_contents读取它,但是没有成功

Works [Tested] . 作品[已测试] You got a bad request since the URL was not encoded. 由于未对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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM