腾讯的ip地址api接口地址:http://fw.qq.com/ipaddress,返回的是数据格式为:var ipdata = new array(123.124.2.85,,北京市,);,一个javascript的对象,目前还不知道如何输入ip查询。
新浪的ip地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=123.124.2.85
网易有道的ip地址查询接口:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=123.124.2.85 使用js代码进行调取腾讯的api接口:
//腾讯api的php调用方法function getipplace(){ $ip=file_get_contents(http://fw.qq.com/ipaddress); $ip=str_replace('',' ',$ip); $ip2=explode((,$ip); $a=substr($ip2[1],0,-2); $b=explode(,,$a); return $b;}$ip=getipplace();print_r($ip);//调用查询接口需要抓取网页,有三种方法,第一种是curl,第二种是//file_get_contents,第三种fopen->fread->fclose,推荐第二种方法/* *根据腾讯ip分享计划的地址获取ip所在地,比较精确 */function getiploc($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地址,使用file_get_contents抓去网页
function getiploc($queryip){$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip;$result = file_get_contents($url); $result = mb_convert_encoding($result, utf-8, gb2312); // 编码转换,否则乱码preg_match(@(.*)@iu,$result,$iparray);$loc = $iparray[1];return $loc;}//根据腾讯接口查询ip地址,使用fopen->fread->fclose抓去网页function getiploc($queryip){$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip;$handle = fopen ($url, rb); $result = ; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $result .= $data; } while(true); $result = mb_convert_encoding($result, utf-8, gb2312); // 编码转换,否则乱码preg_match(@(.*)@iu,$result,$iparray);$loc = $iparray[1];return $loc;}
复制代码
/********注:1,使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = on,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。2,使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而 且需要拷贝ssleay32.dll和libeay32.dll到c:\windows\system32下;linux下要安装curl
扩展*****///新浪查询ip接口 第五个第六个是地理信息function getiploc($ip_ip){$ip_str = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$ip_ip); if(!empty($ip_str)){ $ip_tmp = explode( , $ip_str); $ip_city = iconv(gbk, utf-8, $ip_tmp[5]);return $ip_city;} //有道api的php调用方法$url = http:www.youdao.com/smartresult-xml/search.s?type=ip&q=.$ip;$doc = new domdocument(); $doc->load($url); $smartresult = $doc->getelementsbytagname(product); foreach($smartresult as $product) { $locations = $product->getelementsbytagname(location); $location = $locations->item(0)->nodevalue; } if($location != ) { echo $i...$ip; echo 来自.$location.的网友; } else { echo $i...$ip; echo 来自火星的网友; }public function sinaipapi($ip){ $str = file_get_contents(http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=.$ip); $str = iconv(gbk, utf-8//ignore, $str); preg_match_all(/[\x{4e00}-\x{9fa5}]+/u,$str,$get); $add = implode('',$get[0]); return $add;}//$get是一个非常棒的二维数组
复制代码
新浪api也可以像腾讯api那样用file_get_contents()函数获取完地址后使用一连串的字符串函数处理,使用正则表达式从新浪的返回结果中提供包含中文的字符串,并且分段存入一个二维数组,这个可能只是针对新浪的api有用并且存在bug。
举个例子,查询学校分配给我的ip地址后var_dump()一下函数中$get变量得到以下结果:array(1) { [0]=> array(6) { [0]=> string(6) 中国 [1]=> string(6) 北京 [2]=> string(6) 北京 [3]=> string(9) 教育网 [4]=> string(6) 学校 [5]=> string(18) 中国地质大学 } },而函数输出的结果则是“中国北京北京教育网学校中国地质大学”,希望我的思路和方法能对别人有用。
