I’m trying to call a GET REST API, the problem is that the remote server gives me a HTTP 400 error. But when I put the link to my browser, it returns me the right response. It’s something wrong with my code?
The API I’m using is ArcGIS findAddressCandidates
Here’s the function i used:
function callAPI_GET($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$result = curl_exec($curl);
if ($result) {
// Returns result
return $result;
} else {
// Returns error
return "Errore durante l'esecuzione della richiesta: " . curl_error($curl);
}
}
Here’s the call:
$geo_request = callAPI_GET('https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?city=' . $comune . '&SingleLine=' . $address_param . '&sourceCountry=ITA&maxLocations=1&f=json&outFields=location&token=*MY TOKEN*');
2
Answers
We can only speculate. Maybe the target api has a user-agent whitelist? maybe it only responds 200 to
Accept: text/html
? maybe it needs a pre-existing cookie session? whatever the case, see my answer here: https://stackoverflow.com/a/55829622/1067003or better yet, check the api documentation.
it’s because of SSL, your localhost doesn’t have SSL installed so you have to add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0)
where verifying your SSL is not required.