区县级IP数据查询

  • 2017-07-26
  • 0
  • 6

为QQ显IP版插件开发API接口时,考虑到接口调用频率高会产生高额API费用,而采用免费版接口则达不到要求的精确度,故下面这段代码应运而生。

<?php
header('Content-type: application/json;charset=utf-8');
if(!isset($_REQUEST['ip'])||empty($_REQUEST['ip'])){exit;}
$status='success';
$msg='';
function XCURL($url,$post,$referer){
        $ip = mt_rand(1,255).".".mt_rand(1,255).".".mt_rand(1,255).".".mt_rand(1,255)."";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array("CLIENT-IP".$ip."", "X_FORWARD_FOR:".$ip."")); 
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
        curl_setopt($ch, CURLOPT_TIMEOUT,90);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        curl_setopt($ch, CURLOPT_REFERER, $referer);  
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);  
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        $result=curl_exec($ch);
        curl_close($ch);
        return $result;
    }
function trimall($str)
{
    $qian=array(" "," ","\t","\n","\r");$hou=array("","","","","");
    return str_replace($qian,$hou,$str);    
}
    $url='http://www.ipip.net/ip.html';
	$referer='http://www.ipip.net/ip.html';
    $post=array(
        '_verify'=>'45df31f3738bba21f9c1579e4452b38e',
        'ip'=>$_REQUEST['ip']
    );
	$contents=XCURL($url,http_build_query($post),$referer);
	$alpharegular='/<span id="myself">(.*?)<\/span>/is';
	$betaregular='/<div style="text-align: center;color:red;font-size: 20px;font-weight: 600;">(.*?)<\/div>/is';
	preg_match_all($alpharegular,$contents,$alphatables);
	preg_match_all($betaregular,$contents,$betatables);
	if(!empty($alphatables[1][0])){
	if(empty($betatables[1][0])){
		$json=array('status:'=>$status,'msg:'=>$msg,'county:'=>'0','data:'=>trimall($alphatables[1][0]));
	}else{
		$json=array('status:'=>$status,'msg:'=>$msg,'county:'=>'1','data:'=>array(trimall($alphatables[1][0]),trimall($betatables[1][0])));
	}
	}else{
        $msg='Non Info';
		$json=array('status:'=>$status,'msg:'=>$msg,'county:'=>'0','data:'=>'');
	}
	echo json_encode($json);
?>

评论

CC共享许可协议:BY-NC-SA 4.0 - 友情链接 - Theme by Qzhai