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

问题描述 投票:0回答:1

如何在不使用任何其他服务的情况下通过 IP 获取用户的 ISP、国家和城市。我用whatismyipaddress.com查看了一些解决方案,但它们不再允许通过curl返回http://whatismyipaddress.com/ip/194.153.145.104 -

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 不允许获取地理位置数据。这是我测试过的:

$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;
php curl ip
1个回答
-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');
© www.soinside.com 2019 - 2024. All rights reserved.