Reputation: 573
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?
Upvotes: 6
Views: 8642
Reputation: 2574
I tried the solution suggested by rescue1155 but it did not work.
urlencode($address)
did work for me.
Upvotes: 0
Reputation: 21
That error is caused by white spaces, so make sure that your $address_string does not have a white space.
Upvotes: 0
Reputation: 415
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.
Upvotes: 19