PHP curl bad request 400 - mapquest geocoding

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 is long, but it must comply 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 contains the url value above. First exit:

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

The output of the second request:

 { "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": [] }] } 

These two different cURL queries return basically the same thing. When I run cURL from my terminal on my local machine, 400 is returned.

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

What is the difference between a request for a browser and cURL?

+4
source share
2 answers

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

The only difference was spaces. I tried to run the php function urlencode() as well as the php rawurlencode() function and did not affect the result.

This is what I use now instead of url encoding:

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

enter image description here

+1
source

The problem seems to be related to how you set the variable $urlForGeocode .. are you using ' or " ?

He must be the first.

The second request fails because the URL field is (space) in which an invalid request occurs.

0
source

Source: https://habr.com/ru/post/1232951/


All Articles