Home >Backend Development >PHP Tutorial >Taobao IP address database API interface (PHP) obtains address information through ip_PHP tutorial

Taobao IP address database API interface (PHP) obtains address information through ip_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:151384browse

Here we introduce the method of using Taobao IP address library API interface (PHP) to obtain address information through IP. We have introduced Sina before, and you can introduce Taobao here.

Taobao IP address library website: http://ip.taobao.com/

Taobao IP address database API interface (PHP) obtains address information through ip_PHP tutorial

Services provided include:
1. Based on the IP address provided by the user, quickly query the geographic information and geographic-related information where the IP address is located, including country, province, city and operator.
2. Users can update our service content based on their location and IP address.

Interface description:
1. Request interface (GET method):
http://ip.taobao.com/service/getIpInfo.php?ip=[ip address string]

2. Response information (json format data):
Country, province (autonomous region or municipality), city (county), operator

3. Return data format:

The code is as follows Copy code
{"code":0,"data":{"ip":"210.75.225.254","country":"u4e2du56fd","area":"u534eu5317",
 代码如下 复制代码
{"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"}}
"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"}}

The meaning of the value of code is, 0: success, 1: failure.

4. PHP code example:

 代码如下 复制代码

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

The code is as follows Copy code

function getCity($ip)

{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;

$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
return $data;
}
$ip='221.216.64.183';
print_r(getCity($ip));exit;



5. Get IP address php code


error_reporting (E_ERROR | E_WARNING | E_PARSE);
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]){
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]){
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]){
$ip = $HTTP_SERVER_VARS["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"; } echo $ip; ?>
http://www.bkjia.com/PHPjc/632722.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632722.htmlTechArticleHere we introduce the method of using Taobao IP address library API interface (PHP) to obtain address information through ip, which was introduced earlier Sina, you can introduce Taobao here. Taobao IP address database URL:...
Statement:
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