php,ip获取所在城市名称 PHP代码


# ip获取所在城市名称
<?php
error_reporting(0);
header("Content-Type: text/html;charset=utf-8");
$ip = "27.154.24.153";  //要查询的ip
echo getCity($ip);
function getCity($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    $ipinfo=json_decode($file_contents);
    if($ipinfo->code=='1'){
        return false;
    }
    $city = $ipinfo->data->region.$ipinfo->data->city;
    return $city;
}
签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回