3
votes

I have been struggling a lot to fix an issue related with Google distance matrix API call. Please the below code which I use to calculated distance between two calls. It work perfectly fine in localhost and one website hosted in Hostgator.

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&key=YOUR_API_KEY

$details = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=95864&destinations=91367&key=<API_KEY>' ; 
 
$json = file_get_contents($details);
 	$details = json_decode($json, TRUE); 

 	$distanceinmiles  =  ( $details['rows'][0]['elements'][0]['distance']['value'] * 0.000621371);  

 	if($distanceinmiles > 0)
 	echo "Distance: " . $distanceinmiles  ;
else
	echo "Bad request";

is to be replaced with original API key.

The same code works in http://egressacademy.in/distance.php and shows the distance properly.

However, when the code is checked from another website from Enom hosting provider it failed.

I have also added Key restriction in Google API console for both server IP addresses.

Can anyone please advise how to fix this issues?

1
Have you listed your domain in your Google account? Requests from the domains which you had registered with API key are only considered as valid - Coder
Hi. Yes, I have listed server IP addresses. Both servers IP addresses for the two website. The one hosted in enom is not working while the other hosted in hostgator works fine. - HeroCode
Not the IP address but I am asking about the domain? - Coder
There are also API keys which are specific to domain. Share some knowledge about your API key as well - Coder
Hi. Thanks for your response. I have setup the distance calculator form here at egressacademy.in/distance-calculator.php. It work perfectly fine. But when I use this code in another different domain by Enom, it failed. - HeroCode

1 Answers

0
votes

Its problem with you php apache server. You have to enable

allow_url_fopen = on

enable it then it will work again.