淘宝公司提供了一个很好用的ip地理信息查询接口。在这里:http://ip.taobao.com/
以下这个taobaoipquery类将极大的简化相关的信息查询。
<?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();
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
-
-
管理后台无限级菜单
管理后台菜单遍历很久就想做一个通用的管理后台出来,一直太懒没做,今天开始粗略搞了一下,只是先把框架搭建好,菜单栏...
[阅读全文]
网友评论