php curl Google映射API返回零结果,而浏览器返回中相同的URL



我正在提出php curl请求,但是正如标题所述,它在浏览器中返回零结果,而在浏览器中给出了结果,其他Zipcodes可以使结果curl和浏览器。(我删除了API密钥,因此可以在此处进行测试,但是使用API键的结果相同,因此我不知道您已经超出了每日请求,而只是空数组(。

在下面的浏览器中尝试URL,然后在https://onlinecurl.com/

中curl调用
https://maps.googleapis.com/maps/api/geocode/json?address=4735CC,,&componentRestrictions=country:NL&sensor=false

完整代码:

gives result browser but not in curl
          //https://maps.googleapis.com/maps/api/geocode/json?address=4735CC,,&componentRestrictions=country:NL&sensor=false
gives result in curl & browser
        //https://maps.googleapis.com/maps/api/geocode/json?address=4751AJ,,&componentRestrictions=country:NL&sensor=false

            function getCoordinates($address, $zipcode, $city) {
                $location       = urlencode($address).','.urlencode($zipcode).','.urlencode($city);
                $details_url    = "https://maps.googleapis.com/maps/api/geocode/json?address=".$location;
                usleep(500000);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_HTTPGET,true);
                curl_setopt($ch, CURLOPT_URL, $details_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $response   = json_decode(curl_exec($ch), true);
                var_dump($response);
 // gives {
   "results" : [],
   "status" : "ZERO_RESULTS"
}

我认为问题与您的输入有关!尝试此代码,如果仅设置地址/zipcode/city,则可以使用。可能是Google地图无法找到您的输入地址的组合 Zipcode City。

    function getCoordinates($address, $zipcode = null, $city = null ) {
        // $location       = urlencode($address).','.urlencode($zipcode).','.urlencode($city);
       $location = urlencode($address);
        $details_url    = "https://maps.googleapis.com/maps/api/geocode/json?key=key&address=".$location;
        usleep(500000);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPGET,true);
        curl_setopt($ch, CURLOPT_URL, $details_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response   = json_decode(curl_exec($ch), true);
        var_dump($response); }

刚刚遇到了同样的问题 - 也许此Issuetracker链接对您对我的帮助。我需要指定"接受语言"。标题,该标题是自动为浏览器请求添加的,因此Google可以匹配正确的结果。

https://issuetracker.google.com/issues/35822155

最新更新