php通过淘宝API查询IP地址归属等信息
程序员文章站
2022-03-25 08:00:24
淘宝公司提供了一个很好用的ip地理信息查询接口。
在这里:http://ip.taobao.com/
taobaoipquery2这个类将极大的简化相关的信息查...
淘宝公司提供了一个很好用的ip地理信息查询接口。
在这里:http://ip.taobao.com/
taobaoipquery2这个类将极大的简化相关的信息查询。
类 taobaoipquery2 文件:
<?php /* usage: * $ipinfo = taobaoipquery2::getipinfo('ipaddress'); */ class taobaoipquery2{ private static $_requesturl = 'http://ip.taobao.com/service/getipinfo.php'; public static function getipinfo($ip){ $long = ip2long($ip); if($long === 0){ throw new exception('ip address error', 5); } $ip=long2ip($long); $ipinfo = self::queryipinfo($ip); return self::parsejson($ipinfo); } private static function queryipinfo($ip){ $query = http_build_query(array('ip'=>$ip)); $ch = curl_init(); $options = array( curlopt_url => sprintf('%s?%s', self::$_requesturl, $query), curlopt_returntransfer => true, curlopt_autoreferer => false, curlopt_followlocation => false, curlopt_header => false, curlopt_timeout => 3.0, ); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content; } private static function parsejson($json){ $o = json_decode ($json, true); if(false === is_null($o)){ return $o; } if (version_compare(php_version, '5.3.0', '>=')) { $errorcode = json_last_error(); if(isset(self::$_jsonparseerror[$errorcode])){ throw new exception(self::$_jsonparseerror[$errorcode], 5); } } throw new exception('json parse error', 5); } private static $_jsonparseerror = array( json_error_none=>'no error has occurred', json_error_depth=>'the maximum stack depth has been exceeded', json_error_ctrl_char=>'control character error, possibly incorrectly encoded', json_error_state_mismatch=>'invalid or malformed json', json_error_syntax=>'syntax error', json_error_utf8=>'malformed utf-8 characters, possibly incorrectly encoded', ); }
taobaoipquery2.class.php:
<?php class taobaoipquery2{ private static $_requesturl = 'http://ip.taobao.com/service/getipinfo.php'; public static function getipinfo($ip){ $long = ip2long($ip); if($long === 0){ throw new exception('ip address error', 5); } $ip=long2ip($long); $ipinfo = self::queryipinfo($ip); return self::parsejson($ipinfo); } private static function queryipinfo($ip){ $query = http_build_query(array('ip'=>$ip)); $ch = curl_init(); $options = array( curlopt_url => sprintf('%s?%s', self::$_requesturl, $query), curlopt_returntransfer => true, curlopt_autoreferer => false, curlopt_followlocation => false, curlopt_header => false, curlopt_timeout => 3.0, ); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content; } private static function parsejson($json){ $o = json_decode ($json, true); if(false === is_null($o)){ return $o; } if (version_compare(php_version, '5.3.0', '>=')) { $errorcode = json_last_error(); if(isset(self::$_jsonparseerror[$errorcode])){ throw new exception(self::$_jsonparseerror[$errorcode], 5); } } throw new exception('json parse error', 5); } private static $_jsonparseerror = array( json_error_none=>'no error has occurred', json_error_depth=>'the maximum stack depth has been exceeded', json_error_ctrl_char=>'control character error, possibly incorrectly encoded', json_error_state_mismatch=>'invalid or malformed json', json_error_syntax=>'syntax error', json_error_utf8=>'malformed utf-8 characters, possibly incorrectly encoded', ); }
调用:
$ip = $_server["remote_addr"]; $ipquery = new taobaoipquery($ip); $region = $ipquery->get_region(); $country = $ipquery->get_country(); $city = $ipquery->get_city();
我们再来看个腾讯api接口的
/* *根据腾讯ip分享计划的地址获取ip所在地,比较精确 */ function getiploc_qq($queryip){ $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip; $ch = curl_init($url); curl_setopt($ch,curlopt_encoding ,'gb2312'); curl_setopt($ch, curlopt_timeout, 10); curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回 $result = curl_exec($ch); $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码 curl_close($ch); preg_match("@<span>(.*)</span></p>@iu",$result,$iparray); $loc = $iparray[1]; return $loc; }
腾讯的ip分享计划的查询接口连接是:,该连接后面会带有一个get参数searchip1,即searchip1=你要查询的ip地址。采用php向发送http get request,然后获取相应,获得相应结果后,将结果用正则表达式将需要的地理位置信息提取出来就ok了。当然,php发送get请求的方法有很多,我用curl来模拟http请求的方法
curl是php的一个扩展,进行测试之前,先确认php已经加载了curl扩展。打开php配置文件php.ini,搜索 “extension=php_curl.dll”,将其前面的分号(;)去掉,然后重启http服务器,查看phpinfo(),若能看到curl的信 息,说明php扩展curl成功
接下来是新浪网的api接口
新浪的ip查询接口地址是:,该连接后面也带有get参数,参数ip=你要查询的ip地址,format=返回的查询结果格式。为了方便起见,返回的格式采用json,即format=json,因此,php出来扩展curl外,还得扩展json:
/* *根据新浪ip查询接口获取ip所在地 */ function getiploc_sina($queryip){ $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryip; $ch = curl_init($url); //curl_setopt($ch,curlopt_encoding ,'utf8'); curl_setopt($ch, curlopt_timeout, 10); curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回 $location = curl_exec($ch); $location = json_decode($location); curl_close($ch); $loc = ""; if($location===false) return ""; if (emptyempty($location->desc)) { $loc = $location->province.$location->city.$location->district.$location->isp; }else{ $loc = $location->desc; } return $loc; }
使用getiploc_sina("183.37.209.57")即可得到ip地址所在的地址位置。
上一篇: 超级搞笑内涵笑话