Is There a Good PHP Geolocation Service

is there a good PHP geolocation service?

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.

I want to find current location of user in php

If you don't want to use the HTML5 Geolocoation API, you can detect a bit from IP.

Or try other other solution

The Google Geocoding API

Geolocator-PHP

Maxmind http://www.maxmind.com/app/php

ipinfodb

Geo Location based on IP Address - PHP

Get Geo-IP Information

Requests a geo-IP-server (netip.de) to check, returns where an IP is located (host, state, country, town).

<?php
$ip='94.219.40.96';
print_r(geoCheckIP($ip));
//Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

//Get an array with geoip-infodata
function geoCheckIP($ip)
{
//check, if the provided ip is valid
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}

//contact ip-server
$response=@file_get_contents('http://www.netip.de/search?query='.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}

//Array containing all regex-patterns necessary to extract ip-geoinfo from page
$patterns=array();
$patterns["domain"] = '#Domain: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';

//Array where results will be stored
$ipInfo=array();

//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}

return $ipInfo;
}

?>

GeoLocation API

Why don't you map each CountryCodeXXCountryCode to the respective continent? Shouldn't be too hard since there are only about 200 countries and 5 or 7 continents.

EDIT: I have no knowledge of such prepared database in PHP, but here is a json_encode'd array for all the countries with the country code as key and continent name as value:

$json = '{"AD":"Europe","AE":"Asia","AF":"Asia","AG":"North America","AI":"North America","AL":"Europe","AM":"Asia","AN":"North America","AO":"Africa","AQ":"Antarctica","AR":"South America","AS":"Australia","AT":"Europe","AU":"Australia","AW":"North America","AZ":"Asia","BA":"Europe","BB":"North America","BD":"Asia","BE":"Europe","BF":"Africa","BG":"Europe","BH":"Asia","BI":"Africa","BJ":"Africa","BM":"North America","BN":"Asia","BO":"South America","BR":"South America","BS":"North America","BT":"Asia","BW":"Africa","BY":"Europe","BZ":"North America","CA":"North America","CC":"Asia","CD":"Africa","CF":"Africa","CG":"Africa","CH":"Europe","CI":"Africa","CK":"Australia","CL":"South America","CM":"Africa","CN":"Asia","CO":"South America","CR":"North America","CU":"North America","CV":"Africa","CX":"Asia","CY":"Asia","CZ":"Europe","DE":"Europe","DJ":"Africa","DK":"Europe","DM":"North America","DO":"North America","DZ":"Africa","EC":"South America","EE":"Europe","EG":"Africa","EH":"Africa","ER":"Africa","ES":"Europe","ET":"Africa","FI":"Europe","FJ":"Australia","FK":"South America","FM":"Australia","FO":"Europe","FR":"Europe","GA":"Africa","GB":"Europe","GD":"North America","GE":"Asia","GF":"South America","GG":"Europe","GH":"Africa","GI":"Europe","GL":"North America","GM":"Africa","GN":"Africa","GP":"North America","GQ":"Africa","GR":"Europe","GS":"Antarctica","GT":"North America","GU":"Australia","GW":"Africa","GY":"South America","HK":"Asia","HN":"North America","HR":"Europe","HT":"North America","HU":"Europe","ID":"Asia","IE":"Europe","IL":"Asia","IM":"Europe","IN":"Asia","IO":"Asia","IQ":"Asia","IR":"Asia","IS":"Europe","IT":"Europe","JE":"Europe","JM":"North America","JO":"Asia","JP":"Asia","KE":"Africa","KG":"Asia","KH":"Asia","KI":"Australia","KM":"Africa","KN":"North America","KP":"Asia","KR":"Asia","KW":"Asia","KY":"North America","KZ":"Asia","LA":"Asia","LB":"Asia","LC":"North America","LI":"Europe","LK":"Asia","LR":"Africa","LS":"Africa","LT":"Europe","LU":"Europe","LV":"Europe","LY":"Africa","MA":"Africa","MC":"Europe","MD":"Europe","ME":"Europe","MG":"Africa","MH":"Australia","MK":"Europe","ML":"Africa","MM":"Asia","MN":"Asia","MO":"Asia","MP":"Australia","MQ":"North America","MR":"Africa","MS":"North America","MT":"Europe","MU":"Africa","MV":"Asia","MW":"Africa","MX":"North America","MY":"Asia","MZ":"Africa","NA":"Africa","NC":"Australia","NE":"Africa","NF":"Australia","NG":"Africa","NI":"North America","NL":"Europe","NO":"Europe","NP":"Asia","NR":"Australia","NU":"Australia","NZ":"Australia","OM":"Asia","PA":"North America","PE":"South America","PF":"Australia","PG":"Australia","PH":"Asia","PK":"Asia","PL":"Europe","PM":"North America","PN":"Australia","PR":"North America","PS":"Asia","PT":"Europe","PW":"Australia","PY":"South America","QA":"Asia","RE":"Africa","RO":"Europe","RS":"Europe","RU":"Europe","RW":"Africa","SA":"Asia","SB":"Australia","SC":"Africa","SD":"Africa","SE":"Europe","SG":"Asia","SH":"Africa","SI":"Europe","SJ":"Europe","SK":"Europe","SL":"Africa","SM":"Europe","SN":"Africa","SO":"Africa","SR":"South America","ST":"Africa","SV":"North America","SY":"Asia","SZ":"Africa","TC":"North America","TD":"Africa","TF":"Antarctica","TG":"Africa","TH":"Asia","TJ":"Asia","TK":"Australia","TM":"Asia","TN":"Africa","TO":"Australia","TR":"Asia","TT":"North America","TV":"Australia","TW":"Asia","TZ":"Africa","UA":"Europe","UG":"Africa","US":"North America","UY":"South America","UZ":"Asia","VC":"North America","VE":"South America","VG":"North America","VI":"North America","VN":"Asia","VU":"Australia","WF":"Australia","WS":"Australia","YE":"Asia","YT":"Africa","ZA":"Africa","ZM":"Africa","ZW":"Africa"}';

Just call it with:

echo '<pre>';
print_r(json_decode($json, true));
echo '</pre>';

is there a web service that can detect the country of a visitor in PHP?

I have used the following SOAP GeoIPService to good effect from webservicex.net.

You will also of course need to enable SOAP support in your PHP config.

You could then try something like:

$client = new SoapClient("http://www.webservicex.net/geoipservice.asmx?WSDL");
$params = new stdClass;
$params->IPAddress = 'xxx.xxx.xxx.xxx';
$result = $client->GetGeoIP($params);
// Check for errors...
$country = $result->GetGeoIPResult->CountryName;


Related Topics



Leave a reply



Submit