5

i am looking for a good PHP geolocation service, so when i start my app i can have something like:

<?php
$service = new GeoLocService();
echo $service->getLat();
echo $service->getLng();
?>

to retrieve the position of the user in latitude and logitude values.

Raul Leaño Martinet
  • 2,035
  • 6
  • 28
  • 44

4 Answers4

10

Geo-location would need to be done in the browser, since the server (ie PHP) would never get to find out most of the relevant data. The main API for doing geolocation in the browser is Google's, but you as a developer wouldn't need to use that API; you'd use the browser's API, which is standardised (though only supported in a few browsers so far).

The most useful information you'll get for locating a user at the server end would be their IP address, which is often sufficient to locate them down to the nearest town. There are IP-to-nation and IP-to-location databases and APIs which you can download to help with this. Some are free, but to get the town-level locations, you'll be paying for the service. Whichever you go with, they do need to be kept up-to-date though, because IP ranges are often re-allocated.

The browser-level APIs work by having the browser scan the area for Wi-Fi access points. It sends the list of access points to Google, who then use that to work out the Lat/Long (often with alarmingly accurate results).

How do they do that? You may remember Google getting into trouble recently for sucking data from people's Wi-Fis? What they were actually trying to do was map the world's Wi-Fi access points for this geolocation feature.

It does, of course, require the client to have Wi-Fi enabled.

Note that your browser doesn't give direct access to this list of access points to the client (ie Javascript or the DOM), so you can't post it to PHP to get the location using this method via the server; it would have to be done at the client-end.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Also remember that even if you use JavaScript code to get a users geo location, a user doesn't necessarily have to give you access to their location. Currently, every browser always asks if a user wants to give their location to the webpage they're visiting. – Sebastian Jan 18 '11 at 14:27
  • 2
    what do you think about this: 1. retrieve the user location using google API, 2. send that info using a Request to PHP, 3. PHP analyses the info and returns data related to that location, 4. JS receives that data and shows a map with the location of the user and the data related to it. – Raul Leaño Martinet Jan 18 '11 at 14:44
  • @leanyo martinet - Yep, that would work. That's pretty much how it's generally done. – Spudley Jan 18 '11 at 14:46
  • Current (2010/2011) browser support for geolocation, better than I thought it was: http://veerasundar.com/blog/2010/02/geolocation-in-html5-browser-and-device-support/ – AJJ Mar 02 '11 at 09:41
3

MaxMind PHP API isn't bad... http://www.maxmind.com/app/php

kieran
  • 2,304
  • 4
  • 28
  • 44
  • thanks!, i have reviewed the site, i can see that it retrieves the country and city name, do you know if it can get the lat and lng values too? – Raul Leaño Martinet Jan 18 '11 at 14:19
  • I was going to recommend it also. There is also a class for using it http://pear.php.net/package/Net_GeoIP/ I use it, it works well – Dmitri Jan 18 '11 at 14:27
  • @leanyo martinet - this is one of the IP-to-location database products that I described in my answer. The best you're ever going to get from these products is going to be a city name (and even then it isn't guaranteed to be accurate) – Spudley Jan 18 '11 at 14:28
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Apr 24 '17 at 19:34
1

Read the following docs on Geo-Loacaion API: http://dev.w3.org/geo/api/spec-source-v2.html

I'm sure that the above docs will be very useful for you.

Piccolo
  • 1,612
  • 4
  • 22
  • 38
Manish Trivedi
  • 3,481
  • 5
  • 23
  • 29
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Apr 24 '17 at 19:34
0

PHP get Real IP Address, Countries, City, State, Languages of Visitors

You can detect real ip, visitor proxy, languages, state, city too. But for PHP Code, you have to download PHP Source Code from Github.

<?php
// Required Libraries
require_once("ip.codehelper.io.php");
require_once("php_fast_cache.php");

// New Class
$_ip = new ip_codehelper();

// Detect Real IP Address & Location
$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

// Output result
echo $visitor_location['Country']."
";
echo "<pre>";
print_r($visitor_location);

In your case, you $visitor_location['RegionName'] from the result returned.

Ken Le
  • 1,787
  • 2
  • 22
  • 34