通过ip获取用户的ISP,国家和城市?

时间:2014-09-17 10:10:41

标签: ip

如何通过ip获取用户的ISP,国家和城市,而无需任何其他服务。我在whatismyipaddress.com中查找了一些解决方案,但他们不再允许http://whatismyipaddress.com/ip/194.153.145.104通过curl返回 - You appear to be an automated script. Site terms and conditions do not allow for automated/script access. For API details see http://whatismyipaddress.com/api 他们的API不允许获取Geolocation数据。这是我测试过的:

$ip='194.153.145.104';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://whatismyipaddress.com/ip/' . $ip); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
$data = explode("\n", curl_exec($curl));

$isp = null;
$country = null;
$city = null;
$MaxIndex = count($data) - 1;
for ($i = 0; $i < $MaxIndex; $i++){
    if (strpos($data[$i], '<th>ISP:</th>') !== false){
        $isp = str_replace('<td>', '', $data[$i + 1]);
        $isp = str_replace('</td>', '', $isp);
        break;
    }

    if (strpos($data[$i], '<th>Country:</th>') !== false){
        $country = str_replace('<td>', '', $data[$i + 1]);
        $country = str_replace('</td>', '', $country);
        break;
    }

    if (strpos($data[$i], '<th>City:</th>') !== false){
        $city = str_replace('<td>', '', $data[$i + 1]);
        $city = str_replace('</td>', '', $city);
        break;
    }
}
print_r($data);
echo "ISP: ".$isp."<br />Country: ".$country."<br />City: ".$city;

1 个答案:

答案 0 :(得分:-1)

他们可能会检查请求中的User-Agent标头是否是有效浏览器的标头。

将此添加到您的脚本中它应该有效:

curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');