淘宝ip地址查询类(使用淘宝ip库API接口)
我们在网站建设过程中经常需要用到显示访客IP的功能,这里提供一个简单的淘宝IP地址查询类,方便调用。
以下这个taobaoIPQuery类极大的简化了我们IP查询的过程
<?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 be 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'}; } }
调用很简单
$ip = $_SERVER["REMOTE_ADDR"]; $ipquery = new taobaoIPQuery($ip); $region = $ipquery->get_region(); $country = $ipquery->get_country(); $city = $ipquery->get_city();
顶(1)
踩(0)
- 最新评论