Taobao ip address library API interface (PHP) obtains address information through ip address

Source: Internet
Author: User
Tags php code php sample code

Taobao IP Address Library Web site: http://ip.taobao.com/

The provided services include:
1. Based on the IP address provided by the user, quickly query the geographic information and geographical information of the IP address, including the country, province, city, and carrier.
2. You can update our service content based on your 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. Returned data format:

The code is as follows: Copy code
{"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 "}}

The meaning of the code value is: 0: successful, 1: failed.
4. PHP sample code:

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 = '1970. 216.64.183 ';
Print_r (getCity ($ ip); exit;

5. Get the php code of the IP address

<? Php
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;
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.