Google Maps PHP CURL issue “Your client has issued a malformed or illegal request. That’s all we know.”

ε祈祈猫儿з 提交于 2020-01-11 05:15:48

问题


I have a function which returns the coordinates of an address using CURL and the Google Maps API.

The code is as follows:

function get_coordinates($address_string) {

    $address = urlencode($address_string);
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&key=" . $api_key;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response);
    $status = $response_a->status;

    return $response;

}

The code works fine for me and 99% of the web servers I use it on - but for around 1% of servers Google returns the error message:

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

I've checked and the Google API key is correct, PHP CURL is enabled and the PHP version matches PHP versions it is working on.

Can anyone think of any other things which could be causing Google to return this message?


回答1:


Its Because of your $address variable has address with white space, use str_replace and replace the white space with + sign.

It will work, i was facing same problem and fixed this way.




回答2:


That error is caused by white spaces, so make sure that your $address_string does not have a white space.



来源:https://stackoverflow.com/questions/47278922/google-maps-php-curl-issue-your-client-has-issued-a-malformed-or-illegal-reques

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!