PHP中實現擷取IP和地理位置類分享_PHP教程

來源:互聯網
上載者:User

PHP中實現擷取IP和地理位置類分享


這篇文章主要介紹了PHP中實現擷取IP和地理位置類分享,本文地理定位使用的是SINA的通用介面,需要的朋友可以參考下

發現之前有一個指令碼沒有寫完,簡單擴充了一下。
擷取IP使用的是經典的邏輯,地理定位使用的是SINA的通用介面。

使用方法詳見源碼:

代碼如下:


/*
* GET LOCATION BY SINA IP INTERFACE
*
* @SOULTEARY 2013.04.14
*/
classIP
{
private $args=array();

function__construct()
{
$this->args=self::init_args(func_get_args());
$ip=self::GetIP();

$ret=preg_match_all('/(\d+\.){3}\d+/i',$ip,$result);
if(!$ret){
returnfalse;
}else{
$result=$result[0];
}

if(isset($this->args['ONLYIP'])&&$this->args['ONLYIP']==true){

if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){
$result=json_encode($result);
}else{
$result=implode(',',$result);
}
if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){
echo$result;
returntrue;
}else{
return$result;
}
}else{

$apiURL='http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$result[0];
if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){
$apiURL.='&format=json';
$return=$this->ipCURL($apiURL);
}else{
$return=$this->ipCURL($apiURL);
$return=iconv("GBK//IGNORE","UTF-8",$return);
}

if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){
echo$return;
returntrue;
}else{
return$return;
}

}

}

public functioninit_args($args)
{
$result=array();
for($i=0,$n=count($args);$i<$n;$i++){
$result=self::associative_push($args[$i],$result);
}
return$result;
}

public functionassociative_push($arr,$tmp)
{
if(is_array($tmp)){
foreach($tmpas$key=>$value){
$arr[$key]=$value;
}
return$arr;
}
returnfalse;
}

public functionGetIP()
{
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&

$_SERVER['HTTP_X_FORWARDED_FOR']&&strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'],'unknown')){
return$_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&

strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){
return$_SERVER['REMOTE_ADDR'];
}
}

private functionipCURL($url)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
ob_start();
curl_exec($ch);
curl_close($ch);
$result=ob_get_contents();
ob_end_clean();
return$result;
}
}

?>




demo



CODE:



GETIP



'ONLYIP'=>true,'ECHO'=>true



true,'ECHO'=>true));?>



'ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true



true,'FORMAT'=>'JSON','ECHO'=>true));?>



GET Location



'ECHO'=>true



true));?>



'FORMAT'=>'JSON','ECHO'=>true



'JSON','ECHO'=>true));?>



http://www.bkjia.com/PHPjc/955834.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/955834.htmlTechArticlePHP中實現擷取IP和地理位置類分享 這篇文章主要介紹了PHP中實現擷取IP和地理位置類分享,本文地理定位使用的是SINA的通用介面,需要的朋友...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.