您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php通过淘宝API查询IP地址归属等信息,_PHP教程

2024/4/17 10:36:04发布4次查看
php通过淘宝api查询ip地址归属等信息,淘宝公司提供了一个很好用的ip地理信息查询接口。
在这里:http://ip.taobao.com/
taobaoipquery2这个类将极大的简化相关的信息查询。
类 taobaoipquery2 文件:
$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:
$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(@(.*)
@iu,$result,$iparray); $loc = $iparray[1]; return $loc; }
腾讯的ip分享计划的查询接口连接是:http://ip.qq.com/cgi-bin/searchip,该连接后面会带有一个get参数searchip1,即searchip1=你要查询的ip地址。采用php向http://ip.qq.com/cgi-bin/searchip发送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查询接口地址是:http://int.dpool.sina.com.cn/iplookup/iplookup.php,该连接后面也带有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地址所在的地址位置。
您可能感兴趣的文章:php 手机归属地查询 apiphp实现手机归属地查询api接口实现代码php中根据ip地址判断城市实现城市切换或跳转代码php手机号码归属地查询代码(api接口/mysql)php验证手机号码(支持归属地查询及编码为utf8)php转换ip地址到真实地址的方法详解php根据ip地址获取所在城市具体实现免费手机号码归属地api查询接口和php使用实例分享php通过api获取手机号码归属地
http://www.bkjia.com/phpjc/1084539.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1084539.htmltecharticlephp通过淘宝api查询ip地址归属等信息, 淘宝公司提供了一个很好用的ip地理信息查询接口。 在这里:http://ip.taobao.com/ taobaoipquery2这个类将极大...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录