5

My url is:

http://www.mapquestapi.com/geocoding/v1/batch?key=dNBvDLtTx85L3akdg8vBoHQXrWpDJSEI&location=HEBRON,KY,US&location=CINCINNATI,KY,US&location=CINCINNATI,KY,US&location=BEDFORD PARK,IL,US&location=BEDFORD PARK,IL,US&location=HODGKINS,IL,US&location=HODGKINS,IL,US&location=HODGKINS,IL,US&location=BALDWIN PARK,CA,US&location=BALDWIN PARK,CA,US&location=BALDWIN PARK,CA,US&location=,,US

It's long but it should be compliant with https://www.mapquestapi.com/geocoding/

The mystery unfolds when I run the following php code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $urlForGeocode);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);


$return = curl_exec ($ch);
curl_close ($ch);
echo( $return);


$output = shell_exec('curl ' . $urlForGeocode);
echo( $output);

The variable $urlEncode holds the value of the url above. The first output is:

<html><body><b>Http/1.1 Bad Request</b></body> </html>

The output of the second request is:

{
    "info": {
        "statuscode": 400,
        "copyright": {
            "text": "\u00A9 2016 MapQuest, Inc.",
            "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
            "imageAltText": "\u00A9 2016 MapQuest, Inc."
        },
        "messages": ["no 'json' parameter found"]
    },
    "options": {
        "maxResults": -1,
        "thumbMaps": true,
        "ignoreLatLngInput": false
    },
    "results": [{
        "providedLocation": {},
        "locations": []
    }]
}

Those two different cURL requests return basically the same thing. When I run the cURL from my terminal on my local machine, a 400 is returned as well.

If I put the URL in my browser though, I'm not disappointed, and it returns the data I'm looking for with a 200 status code.

What is different between a browser get request and the cURL?

stackOverFlew
  • 1,479
  • 2
  • 31
  • 58
  • 3
    Try add `curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));` In chrome, open dev tools > network, find the request and press copy as curl and you can see all the settings it use to make that request so you can compare. – Matt The Ninja Aug 17 '16 at 11:39
  • 1
    have you tried encoding the url, ie, turning spaces into %20? Also, to directly answer something different about a curl request vs browser request, is the user agent. have you tried having curl spoof your browsers user agent? – chiliNUT Aug 17 '16 at 12:21
  • you guys were both right... Huge thanks for the copy as curl. I did that, looked at the URL, and the only thing different was the spaces. I didn't believe that it was just the spaces, so I didn't try that right of the bat. Well it was. Funny though, I tried urlencode() and rawurlencode() both without results. – stackOverFlew Aug 17 '16 at 12:58

2 Answers2

1

Like @Matt the ninja and @chiliNut suggested, I needed to look at the difference between the two requests.

The only difference was the spaces. I tried running the urlencode() php function as well as the rawurlencode() php function, and neither affected the result.

This is what I am using now instead of url encoding:

$urlForGeocodeURLEncoded = str_replace(' ', '%20', $urlForGeocode);

enter image description here

stackOverFlew
  • 1,479
  • 2
  • 31
  • 58
0

Seems to be an issue with the way you set the variable $urlForGeocode .. are you using ' or "?

It should be the former.

The second request fails because there's a (space) in the URL field that produces a malformed request.

FloatingRock
  • 6,741
  • 6
  • 42
  • 75
  • Regarding the url, I have it set as a string surrounded by ' '. I don't think the second one fails because of the space though. The space is necessary to run a command (if you are referring to the shell execution). – stackOverFlew Aug 17 '16 at 12:48