Reputation: 355
I am trying to send json request and store the response in a variable using php using the below code
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=xxx&app_code=yyy&searchtext=3891 Delwood Drive, Powell, OH, United States";
$json = file_get_contents($hmaps_request);
$details = json_decode($json, TRUE);
I could not get anyerrors as well as any response. But if i paste the url in browser i could get json response.
Upvotes: 0
Views: 1711
Reputation: 927
I think the error in your request URL. Please check below code.
$url = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891 Delwood Drive, Powell, OH, United States";
$url = str_replace(" ","%20",$url);
$json = @file_get_contents($url);
$details = json_decode($json, TRUE);
print_r($details);
Upvotes: 1
Reputation: 489
There is an error in your request URL change the request URL
From
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891 Delwood Drive, Powell, OH, United States";
To
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891+Delwood+Drive+Powell+OH+United+States";
Here is the full code
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891+Delwood+Drive+Powell+OH+United+States";
$json = file_get_contents($hmaps_request);
$details = json_decode($json, TRUE);
print_r($details);
Upvotes: 1
Reputation: 7893
If you read the $http_response_header
array (generated by file_get_contents() call), you'd find out that you got a "400 Bad Request" response and thus got nothing. So something must have gone wrong with your URL.
After a brief inspection, I think perhaps its your "searchtext"
parameter that's blocking you. For starter, valid URL query don't usually have spaces in it. Probably why the API server says that you're a "Bad Request".
Normally, you need to properly escape the URL query string to have a proper URL. Modern browsers are very good at translating it for you seamlessly and automatically so you might not have this problem by using the URL directly in browsers.
Let's put this theory to a test. We'll use http_build_query() to escape your query query parameters:
(For privacy reason, the parameters are modified. Please substitute the query variables)
$query = http_build_query([
'app_id' => 'your-app-id',
'app_code' => 'your-app-code',
'searchtext' => '123 Some Address',
]);
$url = 'https://geocoder.api.here.com/6.2/geocode.json?' . $query;
$json = file_get_contents($url);
$details = json_decode($json, TRUE);
var_dump($details);
I seem to get proper decoded response now. Try it yourself.
Upvotes: 2