Can´t properly pass the URL to the Geocoding API

4 posts / 0 new
Last post
sistemaszd
Can´t properly pass the URL to the Geocoding API

Hi,

This is my first attempt to MapQuest and geocoding in general, and it seems that I am not properly passing the url to the API in order to fetch the right coordinates.

 

This is a reference address:
282 Rosario, Caballito, Ciudad Autonoma de Buenos Aires, Argentina

It returns "-34.618943,-58.432977" on the web map demo, but "-34.59058,-58.5135" with my code.

This is the url I build and pass through a variable:

https://www.mapquestapi.com/geocoding/v1/address?key=<MYKEY>&outFormat=j... Rosario, Caballito, Ciudad Autonoma de Buenos Aires, Argentina&thumbMaps=false

And this is the code I am using to fetch coords with the address inside the "$buildQuery2" variable and then parse the response:

        $ch = curl_init(sprintf("$buildQuery2"));
        
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_URL, $buildQuery2);
        curl_setopt($ch, CURLOPT_HTTPGET, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);

        $response = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($response,true);

 

I do get a response and I do parse it, so the code is doing the request. but I suspect I´m probably not encoding the address or the whole url in the appropiate manner?

I´ve tried serveral variations removing commas, replacing spaces and/or ampersands with the corresponding html codes, etc. 

Yet, if I paste my built URL in the browser I get accurate results, so I don´t think I´m too far of getting it to work.

Any help will be appreciated.
Thank you.

Rod.


MQBrianCoakley
Can you use Fiddler or some
Can you use Fiddler or some other network tool to see the request that is leaving your machine and test that? This seems more of a curl issue than the MapQuest API since it works in the browser.

sistemaszd
Hi, I never have, but I could

Hi, I never have, but I could!

But before I dive into that: I've found this post you made a while ago

https://developer.mapquest.com/forum/batch-geocoding-get-vs-post

Where you stated

Here is how I did it in curl:

curl -d "json={\"locations\": [{\"street\": \"555 17th St, Denver,CO\"},{\"street\": \"Baseline Ave, Boulder,CO\"},{\"street\": \"Golden,CO\"}],\"options\": {\"maxResults\": -1,\"thumbMaps\": true,\"ignoreLatLngInput\": false}}" "http://www.mapquestapi.com/geocoding/v1/batch?key=KEY"

 

That is completely different from what I am passing and how I am interpreting the docs (see my passed URL above)... Am I absolutely wrong in my approach or what I am amissing there?

Thanks in advance.

Regards. Rod.


MQBrianCoakley
The json and xml formatted
The json and xml formatted requests use locations. The kvp formatted requests us one from and multiple to parameters.