幾種獲取IP 根據IP獲取地址的方法 JS,第三方 新浪 網易 騰訊


第一種是利用純真ip數據庫,這個可以在網上找到很多,缺點是更新有點慢。
 
第二種是利用門戶網站的接口
 
目前已知的有騰訊、新浪、網易、搜狐和Google提供IP地址查詢API,但是找得到的只有騰訊、新浪和網易的,Google的貌似要用Google Maps所以沒有研究。看了下國內的幾個騰訊提供的是JavaScript的,網易提供的是XML,而新浪的有多種格式可以用,注意非XML的數據源都是GBK格式的,不管是JavaScript調用還是PHP調用都要轉換一下編碼,不然得到的是亂碼。而更需要注意的是,如果一次性查詢多個IP,使用門戶網站的API來查詢會非常緩慢,我大概寫了個for循環試了下,不管是用PHP解析XML還是file_get_contents()函數獲取內容,查詢10次以上會變得非常緩慢,甚至可能超時。
 
騰訊的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接口:
 查看源代碼打印幫
 <script language="javascript" type="text/javascript" src="http://fw.qq.com/ipaddress"></script>
 <script>document.write("你的IP是:"+IPData[0]+",來自:"+IPData[2]);</script>
 //騰訊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,推薦第二種方法
 
 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("@<span>(.*)</span></p>@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("@<span>(.*)</span></p>@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("@<span>(.*)</span></p>@iU",$result,$ipArray);
 $loc = $ipArray[1];
 return $loc;
 }
 

 //新浪查詢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_r("product");
    foreach($smartresult as $product)
    {
       $locations = $product->getElementsByTagName_r("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) "中國地質大學" } },而函數輸出的結果則是“中國北京北京教育網學校中國地質大學”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM