簡體   English   中英

無法使用cuRL從Google Maps請求中獲取JSON數據

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

我正在嘗試使用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-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);
    }

$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讀取它,但是沒有成功

作品[已測試] 由於未對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