Reputation: 27
I'm trying to make a GET Response from HereMaps Geocoding API as follows:
$hmaps_request = "https://geocode.search.hereapi.com/v1/geocode?apiKey={MY_API_KEY}&q=3891+Delwood+Drive%2C+Powell%2C+OH%2C+United+States;
$json_details = json_decode($hmaps_request);
I get successful response when all params are right and in proper JSON Format too. But When I provide wrong API Key I get Error as follows:
local.ERROR:Error occurred while geocoding file_get_contents(https://geocode.search.hereapi.com/v1/geocode?apiKey={MY_API_KEY}&q=3891+Delwood+Drive%2C+Powell%2C+OH%2C+United+States): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
I Tried the same using Postman and got proper error response like this with a 401 Status code:
{
"error": "Unauthorized",
"error_description": "apiKey invalid. apiKey not found."
}
Also tried the code snippets provided by postman, but since its a HTTPS, I have to provide certificate it seems. Any way to get proper error response using php.
Upvotes: 0
Views: 86
Reputation: 2512
You can change this behavior by setting http.ignore_errors
to true.
<?php
$opts = [
"http" => [
'ignore_errors' => true
]
];
$context = stream_context_create($opts);
$hmaps_request = file_get_contents("https://geocode.search.hereapi.com/v1/geocode?apiKey={MY_API_KEY}&q=3891+Delwood+Drive%2C+Powell%2C+OH%2C+United+States", false, $context);
$json_details = json_decode($hmaps_request);
/* Output
object(stdClass)#1 (2) {
["error"]=>
string(12) "Unauthorized"
["error_description"]=>
string(33) "apiKey invalid. apiKey not found."
}
*/
var_dump($json_details);
Upvotes: 1