php通过淘宝ip库批量获得运营商名称

 保存ip的文件,是从nginx日志里面提取出来的,格式为:ipaddr request_time

ips.txt:

99.248.29.64 0.464

99.248.29.64 0.467

99.248.29.64 0.469

99.248.29.64 0.489

99.249.155.70 0.691

99.255.102.207 0.345

99.255.102.207 0.346

99.255.102.207 0.349

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

你可能感兴趣的:(PHP,IP,批量)