2016-02-28 85 views
1

我在使用此代碼時遇到問題。它應該得到訪問者的國家代碼。get_file_contents連接太多

$ipot = $_SERVER['REMOTE_ADDR']; 
$details = json_decode(file_get_contents("http://ipinfo.io/{$ipot}")); 

echo 'Your country region code is '.$details->country.''; 

,我得到這個錯誤:

Warning: file_get_contents(http://ipinfo.io/173.245.48.118): failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests in

+0

將您從ipinfo.io一個429錯誤 - 從消息中返回,它看起來像阻止了你的網站,因爲你的查詢太多了。 – andrewsi

+0

任何想法如何解決它? –

回答

0

看起來像你的服務器是越來越禁止拍過多/頻繁請求ipinfo.io

..可我建議的替代方法? 從http://software77.net/geo-ip/(或者https://raw.githubusercontent.com/divinity76/http_log_parser/master/Ipv4ToCountry.csv的過期版本)下載Ipv4ToCountry.csv文件,

並通過該代碼獲取國家/地區?需要pdo sqlite3;

function ipv4_to_country_v2($ipv4){ 
    static $ip=false; 
    $ipv4=filter_var($ipv4,FILTER_VALIDATE_IP,array('flags'=>FILTER_FLAG_IPV4,'options'=>array('default'=>false))); 
    if($ipv4===false){ 
     throw new InvalidArgumentException('input is NOT a valid ipv4 address.. (sorry, no ipv6 support yet.)'); 
    } 
    //$ip=ip2long($ipv4); 
    $ip=ipv4touint($ipv4); 
    if($ip===false){ 
     throw new UnexpectedValueException('input passed FILTER_FLAG_IPV4, but ip2long could not convert it! should never happen...'); 
    } 
    static $ipdb=false; 
    static $stm=false; 
    if($ipdb===false){ 
     $ipdb=call_user_func(function(){ 
      $ipdb=new PDO('sqlite::memory:','','',array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); 
      $ipdb->query('CREATE TABLE `ipranges` (`iprange_start` INTEGER UNIQUE,`iprange_end` INTEGER UNIQUE,`country` VARCHAR(255));'); 
      assert(is_readable('Ipv4ToCountry.csv')); 
      $iplist_raw=file_get_contents('Ipv4ToCountry.csv'); 
      $matches=array(); 
      $rex_ret=preg_match_all('/\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"\\,\\"([^\\"]*)\\"/',$iplist_raw,$matches); 
      assert($rex_ret>9001); 
      unset($matches[0],$iplist_raw); 
      $iplist=array(); 
      //var_dump($rex_ret,$matches); 
      $ipdb->beginTransaction(); 
      $stm=$ipdb->prepare('INSERT OR IGNORE INTO `ipranges` (`iprange_start`,`iprange_end`,`country`) VALUES(:iprange_start,:iprange_end,:country);'); 
      $stm->bindParam(':iprange_start', $ins_iprange_start, PDO::PARAM_INT); 
      $stm->bindParam(':iprange_end', $ins_iprange_end, PDO::PARAM_INT); 
      $stm->bindParam(':country', $ins_country, PDO::PARAM_STR); 
      for($i=0;$i<$rex_ret;++$i){ 
       //$tmparr=array(); 
       # IP FROM  IP TO  REGISTRY ASSIGNED CTRY CNTRY COUNTRY 
       # "1346797568","1346801663","ripencc","20010601","il","isr","Israel" 
       //$tmparr['HHB_IPRANGE_START']=(int)$matches[1][$i]; 
       //$tmparr['HHB_IPRANGE_END']=(int)$matches[2][$i]; 
       //$tmparr['registry']=$matches[3][$i]; 
       //$tmparr['assigned']=$matches[4][$i]; 
       //$tmparr['ctry']=$matches[5][$i]; 
       //$tmparr['cntry']=$matches[6][$i]; 
       //$tmparr['HHB_COUNTRY']=$matches[7][$i]; 
       //$iplist[]=$tmparr; 
       $ins_iprange_start=$matches[1][$i]; 
       $ins_iprange_end=$matches[2][$i]; 
       $ins_country=$matches[7][$i]; 
       //var_dump('adding: ',$ins_iprange_start,$ins_iprange_end,$ins_country); 
       $stm->execute(); 
      } 
      $ipdb->commit(); 
      return $ipdb; 
     }); 
     $stm=$ipdb->prepare('SELECT `country` FROM `ipranges` WHERE :ip >= `iprange_start` AND :ip <= `iprange_end` LIMIT 1'); 
     //$stm->bindParam(':ip',$ip,PDO::PARAM_INT); 
     $stm->bindParam(':ip',$ip,PDO::PARAM_STR); 
    } 
    $stm->execute(); 
    if(false!==($row=$stm->fetch(PDO::FETCH_ASSOC))){ 
     return $row['country']; 
    } 
    return 'unknown_country'; 
} 
function ipv4touint($ipv4){ 
    return sprintf('%u',ip2long($ipv4)); 
} 

,如果代碼運行你太慢了,保存而不是重新創建它在內存中的每個PHP重啓(上面的代碼是爲一個項目,ipv4_to_country_v2將被稱爲幾十萬,甚至數百萬寫入數據庫次,每次運行和靜態創建數據庫並沒有真正使多大的差別)

編輯:添加ipv4touint

+0

謝謝你,我會試試這個... –