I have a somewhat strange problem. I'm working on a project and need to know the distance between 2 locations in my php script. You can give the google api the city name or the zipcode.
Hey should be easy right? Wrong!
I tried to test it of course with known zipcodes and my city. 'Hannover' and '30159' the distance should be somewhat around 0 but strangly I got over 7200km. (My code to calculate distances between 2 coords works believe me)
Now to the actual problem.
My steps:
Here is my very simple code for that.
$url = "https://maps.googleapis.com/maps/api/geocode/json?&address=".urlencode($ort)."&key=$google_api_key";
echo $url."<hr>";
$string = file_get_contents($url);
echo $string."<hr>";
This is now strange. The output is totally different in the browser when compared to the php script.
output compared browser vs script (sry I have no formatting for the script output)
Is anyone able to explain to me and/or offer solution to make the output of the script like the "desired" output of the browser with the zipcode 30159 located inside germany?
Cheers Nexarius
Ok I figured it out and want to share my solution if anyone has the same problem.
$url = "https://maps.googleapis.com/maps/api/geocode/json?&address=".urlencode($ort)."&key=$google_api_key";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Accept-Language: de-DE-1996"]); // important
$string = curl_exec($curl);
curl_close($curl);
The browser automatically transmits your language when you make an ajax call but the php function did not. With curl you can specify your language and that makes the google api report the values most relevant for your area.