php經過淘寶ip庫批量得到運營商名稱

 保存ip的文件,是從nginx日誌裏面提取出來的,格式爲:ipaddr request_timephp

ips.txt:nginx

99.248.29.64 0.464json

99.248.29.64 0.467api

99.248.29.64 0.469ide

99.248.29.64 0.489url

99.249.155.70 0.691spa

99.255.102.207 0.345日誌

99.255.102.207 0.346code

99.255.102.207 0.349ip

99.255.102.207 0.354

99.255.102.207 0.360

99.255.102.207 0.361

99.255.102.207 0.499

99.255.102.207 0.501

99.255.102.207 0.516

99.255.102.207 0.520

99.255.102.207 0.520

99.255.102.207 0.606

99.255.102.207 0.612

99.255.102.207 0.687

....................

 

ipapi.php:

  
  
  
  
  1. <?php 
  2. function getIsp($ipstr
  3.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ipstr
  4.     $ip=json_decode(file_get_contents($url)); 
  5.     if((string)$ip->code=='1'){ 
  6.       return false; 
  7.     } 
  8.     $data = (array)$ip->data; 
  9.     return $data["isp"]; 
  10. $fh=fopen("$argv[1]","r"); 
  11. while(!feof($fh)){ 
  12.     $line = fgets($fh); 
  13.     $strings = explode(" ",$line); 
  14.     $ip = $strings[0]; 
  15.     $time = $strings[1]; 
  16.     $allip=array(); 
  17.     if(!array_key_exists("$ip",$allip)){ 
  18.         $yunying=getIsp($ip); 
  19.         $allip["$ip"]=$yunying
  20.     }else { 
  21.         $yunying=$allip["$ip"]; 
  22.     } 
  23.     print $yunying."\t".$ip."\t".$time
  24. fclose($fh); 
  25. ?> 

 

腳本使用方法:/usr/bin/php ipapi.php ips.txt

相關文章
相關標籤/搜索