سؤال

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!

نصائح أخرى

I think you're missing the API-Key

Btw, I simply use file_get_contents() for Google's geocoder, since there are no special headers you have to set, or http-redirects you have to follow, etc.

FYI, I just ran into this and my issue was that I had incorrectly typed the "address" query string parameter (I typed it as "adress" rather than "address").

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top