Home >Backend Development >PHP Tutorial >PHP Taobao IP data to obtain user IP and geographical location_PHP tutorial
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
|
Copy code
|
||||||||||
We entered it directly in the IE address above. Here we use the php file_get_contents function to obtain
|
|||||||||||
/**
* 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
|