有道,淘宝 IP库 归属地查询 PHP

2013-08-07 00:00:00 by 【6yang】, 832 visits, 收藏 | 返回

http://www.tension.name/
http://my.oschina.net/quttap/blog/80437

有道,淘宝 IP库 归属地查询 PHP

Jul 27, 2013 13:59 / 归属地查询 IP查询 PHP归属地查询

311

前段日子项目需要,要根据IP查询归属地,当时利用纯真数据库做的,文件太大,代码太多,后来查到有道、百度、淘宝都有类似的IP数据库API提供,但是百度的貌似已经找不到了,淘宝找到相关文档了!!

后来找到有道的API 很好。。。下面就分享给大家。

// 有道归属地查询
ip有更改:http://www.yodao.com/smartresult-xml/search.s?keyfrom=163.com&type=ip&q=192.168.1.1 (yodao.com function getyoudaoip($ip,$num = 1) {
    $ipapi = @file_get_contents("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=".$ip);
    preg_match ('/\\<location\\>(.*?)\\<\\/location\\>/', $ipapi, $location);
    $location = $location[1];
    $address = iconv('GB2312', 'utf-8', $location);
    $address = explode(" ", iconv("gbk", "utf-8", $location));

    if ($num == '1') {
        $add = $address[0];
    } else {
        $add = $address[0]." ".$address[1];
    }
    return $add;
}

淘宝的API

// 淘宝归属地查询
function gettaobaoip($ip,$num = 1) {
    $ipapi = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);
    $ipinfo = json_decode($ipapi, true);

    $country = $ipinfo['data']['country'];  //国家
    $country_id = $ipinfo['data']['country_id'];  //简称
    $area = $ipinfo['data']['area'];  //区域
    $region = $ipinfo['data']['region'];  //省份
    $city = $ipinfo['data']['city'];  //城市
    $isp = $ipinfo['data']['isp'];  //运营商
    $isp_id = $ipinfo['data']['isp_id'];  //运营商ID
    $ips = $ipinfo['data']['ip'];  //IP

    if ($region == $city) {
        $address = $region;
    } else {
        $address = $region . $city;
    }

    if ($num == '1') {
        $add = $country . ' ' . $address;
    } else {
        $add = $country . ' ' . $address . ' ' . $isp;
    }
    return $add;
}

调用

$iP = $_SERVER["REMOTE_ADDR"];
$location = getip($iP,0);

getip($iP,0) 这里getip(string,limit)

分享到:
share

    图片原图

    loading

    loading