Home > php教程 > php手册 > 淘宝IP地址库API接口(PHP)通过ip获取地址信息

淘宝IP地址库API接口(PHP)通过ip获取地址信息

WBOY
Release: 2016-05-25 16:44:52
Original
1500 people have browsed it

淘宝IP地址库网址:http://ip.taobao.com/

提供的服务包括:

1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家、省、市和运营商.

2.用户可以根据自己所在的位置和使用的IP地址更新我们的服务内容.

接口说明:

1. 请求接口(GET方式):

http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
Copy after login

2.响应信息(json格式数据):

国家 、省(自治区或直辖市)、市(县)、运营商

3. 返回数据格式,代码如下:

{"code":0,"data":{"ip":"210.75.225.254","country":"u4e2du56fd","area":"u534eu5317", 
"region":"u5317u4eacu5e02","city":"u5317u4eacu5e02","county":"","isp":"u7535u4fe1", 
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000", 
"county_id":"-1","isp_id":"100017"}}
Copy after login

其中code的值的含义为,0:成功,1:失败.

4. PHP代码示例:

<?php
function getCity($ip) {
    $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
    $ip = json_decode(file_get_contents($url));
    if ((string)$ip->code == &#39;1&#39;) {
        return false;
    }
    $data = (array)$ip->data;
    return $data;
}
$ip = &#39;221.216.64.183&#39;;
print_r(getCity($ip));
exit;
?>
Copy after login

5.获取IP地址php代码

<?php
if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
    $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif ($_SERVER["HTTP_CLIENT_IP"]) {
    $ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif ($_SERVER["REMOTE_ADDR"]) {
    $ip = $_SERVER["REMOTE_ADDR"];
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
    $ip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
    $ip = getenv("HTTP_CLIENT_IP");
} elseif (getenv("REMOTE_ADDR")) {
    $ip = getenv("REMOTE_ADDR");
} else {
    $ip = "Unknown";
}
?>
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template