2016-02-28 86 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

谢谢你,我会试试这个... –