brad62
November 24, 2016, 12:32pm
1
Hello
I am trying to find something that will give the gps coordinates from an address that is being entered.
I found a code that seems to be doing this, but I can’t get it to work.
This is the code that I did put on my test page.
<?php
function getCoordinates($address){
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
$response = file_get_contents($url);
$json = json_decode($response,true);
$lat = $json['results'][0]['geometry']['location']['lat'];
$lng = $json['results'][0]['geometry']['location']['lng'];
return array($lat, $lng);
}
$coords = getCoordinates("Wall Street, New York");
print_r($coords);
?>
But when I upload it to my server and test the page I only get this:
Array ( [0] => [1] => )
What is wrong and how can I fix that?
SamA74
November 24, 2016, 12:38pm
2
A line in the function is commented out. That line defines the variable $url
which is used in the next line.
If the variable is not defined, the function will fail.
Does it work if you uncomment the line?
Edit
I get this result:-
brad62
November 24, 2016, 12:44pm
3
Oh. Sorry. The comment out was just when I tested something. Forgot to remove it.
But it’s not working.
I don’t get any result.
SamA74
November 24, 2016, 12:48pm
4
I don’t know what to suggest, it is working fine here.
brad62
November 24, 2016, 12:55pm
5
This is all the code on my page I upload to test.
Are you also only using this code and have it working?
Or did you add something else to the code?
<?php
function getCoordinates($address){
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
$response = file_get_contents($url);
$json = json_decode($response,true);
$lat = $json['results'][0]['geometry']['location']['lat'];
$lng = $json['results'][0]['geometry']['location']['lng'];
return array($lat, $lng);
}
$coords = getCoordinates("Wall Street, New York");
print_r($coords);
?>
SamA74
November 24, 2016, 12:59pm
6
All I changed was to remove the comment slashes from the original code you posted.
Gandalf
November 24, 2016, 1:11pm
7
I copied the code from your original post (comments presumably already removed) and it works for me.
brad62
November 24, 2016, 1:11pm
8
I am now chatting with the web host. They wonder what php version my code is using. If that might the problem. But I have no idea what php version the code is written in.
SamA74
November 24, 2016, 1:16pm
9
What is the server using?
http://php.net/manual/en/function.phpversion.php
I don’t see anything new and exotic in there.
brad62
November 24, 2016, 1:17pm
10
PHP 5.6 is on the server. But they say I can change it to 7.0
SamA74
November 24, 2016, 1:18pm
11
The script should run fine on 5.6.
brad62
November 24, 2016, 1:23pm
12
The one I’m chatting with is going to ask their tech people.
brad62
November 24, 2016, 1:43pm
13
Seems to be something wrong… one of the tech guys tried my code on his private server and it worked. So I hope they might solve it…
1 Like
brad62
November 24, 2016, 1:54pm
14
They can’t see what is wrong.
Maybe that Google have a limit for this one…
Are you using a free web host? If so, is the tech personal using the same server or is he using his personal server? Usually, file_get_contents
seem to fail at times when it’s used on free web hosting accounts.
brad62
November 24, 2016, 2:01pm
16
No, it’s not a free web host. It’s on one.com
Is there another solution to make it work?
another way to make it work without file_get_contents
SamA74
November 24, 2016, 2:02pm
17
That assumes that is the problem function.
What errors, if any, are reported when the script is run?
brad62
November 24, 2016, 2:03pm
18
I get the following on my screen:
Array ( [0] => [1] => )
SamA74
November 24, 2016, 2:05pm
19
But do you have error reporting? Either on-screen or to a log? That is essential for troubleshooting.
brad62
November 24, 2016, 2:07pm
20
I did turn on PHP Error, but it takes up to 20 mins to see it. So I’m still waiting…