2011-02-05 54 views
2

我想知道有没有人在IP上做whois并从该IP提取国家代码有任何经验?想知道什么是api会是用php做这个最简单的方法。良好的PHP API从IP提取国家代码?

+1

重新标记,您正在寻找地理位置,而不是whois。虽然如此,有用的问题+1 – Endophage 2011-02-05 00:35:36

回答

1

你可以用我的服务,http://ipinfo.io API此:

假设你希望所有的细节,你可以使用PHP的json_decode解析响应:也支持

function ip_details($ip) { 
    $json = file_get_contents("http://ipinfo.io/{$ip}"); 
    $details = json_decode($json); 
    return $details; 
} 

$details = ip_details("8.8.8.8"); 

echo $details->city;  // => Mountain View 
echo $details->country; // => US 
echo $details->org;  // => AS15169 Google Inc. 
echo $details->hostname; // => google-public-dns-a.google.com 

JSONP,所以你可以从JavaScript调用的API:

$.get("http://ipinfo.io", function(response) { 
    console.log(response.city); 
}, "jsonp"); 

更多详细信息可在http://ipinfo.io/developers