I've been trying to get a json response from google's geocoding service. I'm using PHP. I was trying with fopen, then I read in another stackoverflow question that I should use file_get_contents, but didn't worked too. Then I keep searching and found someone in another forum who said I would a better solution if I use CURL so I changed my code and is not working. In all cases I received an "Error 400: Bad Request. Your client has issued a malformed or illegal request."
My code is this:
$jsonUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityName . "&sensor=false";
$geocurl = curl_init();
curl_setopt($geocurl, CURLOPT_URL, $jsonUrl);
curl_setopt($geocurl, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($geocurl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($geocurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($geocurl, CURLOPT_RETURNTRANSFER, 1);
$geofile = curl_exec($geocurl);
Then I print the content and got the error message.
Any ideas?
Thank you very much.
Well, I figured it out.
My $cityName variable was this:
$cityName = "Monterrey, NL";
The white space between the comma and "NL". I used str_replace to change " " for "+" and get a valid url like in the documentation:
http://code.google.com/intl/es/apis/maps/documentation/geocoding/
Greetings and thanks a lot for your help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With