Sometimes we don’t want to use our own database to store IP addresses. Our own IP library is not updated in time. We can directly use a third-party IP library to operate. Here is an introduction to how to use Taobao IP data to obtain user IP and geographical location.
Open the following address directly in IE browser
http://ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8
Return information
The code is as follows
代码如下 |
复制代码 |
{"code":0,"data":{"country":"u7f8eu56fd","country_id":"US","area":"","area_id":"","region":"","region_id":"","city":"","city_id":"","county":"","county_id":"","isp":"","isp_id":"","ip":"8.8.8.8"}}
|
|
Copy code
|
代码如下 |
复制代码 |
/**
* 获取 IP 地理位置
* 淘宝IP接口
* @Return: array
*/
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;
}
|
{"code":0,"data":{"country":"u7f8eu56fd","country_id":"US","area":"","area_id":"","region":" ","region_id":"","city":"","city_id":"","county":"","county_id":"","isp":"","isp_id":"" ,"ip":"8.8.8.8"}}
We entered it directly in the IE address above. Here we use the php file_get_contents function to obtain
代码如下 |
复制代码 |
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
|
The code is as follows |
Copy code |
|
/**
* Get IP geographical location
* Taobao IP interface
* @Return: array
*/
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;
}
The above reason is that the json format data returned by Taobao through file_get_contents is converted into an array using the php json_decode function.
We need to provide the $IP address. Here is a function to obtain the user’s real IP address
The code is as follows
|
Copy code
|
function getIP()
{
static $realip;
If (isset($_SERVER)){
If (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
If (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
Return $realip;
}
http://www.bkjia.com/PHPjc/631546.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631546.htmlTechArticleSometimes we don’t want to use our own database to store IP addresses. Our IP database is not updated in a timely manner. We can use it directly. Use a third-party IP library to operate. Here is an introduction to using Taobao IP data to obtain...
|
|