Taobao Company provides a very useful IP geographic information query interface. Here: http://ip.taobao.com/
The following Taobaoipquery class will greatly simplify the relevant information query.
Copy Code code as follows:
<?php
Class Taobaoipquery {
Private $m _ip;
Private $m _content;
Public function __construct ($IP) {
if (Isset ($IP)) {
$this->m_ip = $ip;
} else {
$this->m_ip = "";
}
if (!empty ($this->m_ip)) {
$url _handle = Curl_init ();
curl_setopt ($url _handle, Curlopt_url, "http://ip.taobao.com/service/getIpInfo.php?ip=". $this->m_ip);
curl_setopt ($url _handle, Curlopt_returntransfer, true);
$this->m_content = curl_exec ($url _handle);
Curl_close ($url _handle);
if ($this->m_content) {
$this->m_content = Json_decode ($this->m_content);
if ($this->m_content->{' code '} = = 1) {
Exit ("Query error!");
}
} else {
Exit ("Curl error!");
}
} else {
Exit ("IP address must is not empty!");
}
}
Public Function get_region () {
return $this->m_content->{' data '}->{' region '};
}
Public Function Get_isp () {
return $this->m_content->{' data '}->{' ISP '};
}
Public Function Get_country () {
return $this->m_content->{' data '}->{' country '};
}
Public Function get_city () {
return $this->m_content->{' data '}->{' city '};
}
}
The call is simple
Copy Code code as follows:
$ip = $_server["REMOTE_ADDR"];
$ipquery = new Taobaoipquery ($IP);
$region = $ipquery->get_region ();
$country = $ipquery->get_country ();
$city = $ipquery->get_city ();