2011-10-14 107 views
21

我們正在尋找一種快速準確的方式來根據IP獲取訪問者位置。基於IP地址的地理位置 - PHP

我們已經嘗試ipinfodb.com,但他們的API使得我們的網站在進行API調用時嚴重滯後。

您建議您提供哪些其他服務?

+0

可能重複:[?服務存在什麼是地理定位基於IP的人(http://stackoverflow.com/questions/464142/what-services-exist-that-geo-locates-a -person-based-on-ip) – hakre

+0

[用PHP獲取IP地址的國家/地區]可能的副本(http://stackoverflow.com/questions/3650006/get-country-of-ip-address-with-php) –

+0

簽出https://ipdata.co,我建立的服務有10個全球終端,每個終端每天能夠處理> 800M的呼叫。它速度超快,並有很多相關數據。 – Jonathan

回答

38

獲取地理信息的IP

請求地理IP服務器(netip.de)檢查,返回其中一個IP所在(主機,州,國家,鎮)。

<?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 
       [email protected]_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: (.*?)&nbsp;#i'; 
       $patterns["country"] = '#Country: (.*?)&nbsp;#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; 
     } 

?> 

Nomsayn?

如果你想有一個數據庫,在這個空間和一個提供企業解決方案是Digital Element使用http://www.phpandstuff.com/articles/geoip-country-lookup-with-php

+1

這很好,但我更喜歡數據庫,因爲我們的網站接收到數千個請求,並且每頁加載都會提供地理IP請求。 – Latox

+2

@latox我剛更新了我的問題,看看那個鏈接應該是你在nomsayn之後? –

+0

@Latox - 如果您使用API​​調用的結果維護本地緩存,那麼您只需爲每個新IP地址支付API調用的開銷。但是,只要您不需要最新的準確性,上述鏈接中的數據庫似乎對您的應用程序來說是完美的。 – Peter

2

的市場領導者。他們提供各種各樣的API,包括PHP,以訪問他們的服務器,您可以在本地安裝或通過Web服務訪問服務器。他們的數據質量很高,解決方案的性能非常好。 MaxMind是另一個可以接受好評的選項。

爲了獲得最佳的準確性,您需要選擇一種服務或解決方案,因爲這些東西在給定的網絡中可能會發生相當大的變化。成本將取決於更新的頻率,地理數據的粒度以及所需的其他字段或數據庫的數量。一些提供商提供語言,人口統計,公司和域名等等。

3

試試這個http://ip.codehelper.io/

這裏是PHP代碼。該代碼會自動緩存您的請求,因此您的服務器不會發送多個請求。返回IP地址,國家,城市和更多信息。

<?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);