VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP中实现获取IP和地理位置类分享

这篇文章主要介绍了PHP中实现获取IP和地理位置类分享,本文地理定位使用的是SINA的通用接口,需要的朋友可以参考下

发现之前有一个脚本没有写完,简单扩充了一下。

获取IP使用的是经典的逻辑,地理定位使用的是SINA的通用接口。

使用方法详见源码:

  1. <?php 
  2. /* 
  3.  * GET LOCATION BY SINA IP INTERFACE 
  4.  * 
  5.  *              @SOULTEARY 2013.04.14 
  6.  */ 
  7. classIP 
  8.     private $args=array(); 
  9.  
  10.     function__construct() 
  11.     { 
  12.         $this->args=self::init_args(func_get_args()); 
  13.         $ip=self::GetIP(); 
  14.  
  15.         $ret=preg_match_all('/(\d+\.){3}\d+/i',$ip,$result); 
  16.         if(!$ret){ 
  17.             returnfalse; 
  18.         }else
  19.             $result=$result[0]; 
  20.         } 
  21.  
  22.         if(isset($this->args['ONLYIP'])&&$this->args['ONLYIP']==true){ 
  23.  
  24.             if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){ 
  25.                 $result=json_encode($result); 
  26.             }else
  27.                 $result=implode(',',$result); 
  28.             } 
  29.             if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){ 
  30.                 echo$result
  31.                 returntrue; 
  32.             }else
  33.                 return$result
  34.             } 
  35.         }else
  36.  
  37.             $apiURL='http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$result[0]; 
  38.             if(isset($this->args['FORMAT'])&&$this->args['FORMAT']=='JSON'){ 
  39.                 $apiURL.='&format=json'
  40.                 $return=$this->ipCURL($apiURL); 
  41.             }else
  42.                 $return=$this->ipCURL($apiURL); 
  43.                 $return=iconv("GBK//IGNORE","UTF-8",$return); 
  44.             } 
  45.  
  46.             if(isset($this->args['ECHO'])&&$this->args['ECHO']==true){ 
  47.                 echo$return
  48.                 returntrue; 
  49.             }else
  50.                 return$return
  51.             } 
  52.  
  53.         } 
  54.  
  55.     } 
  56.  
  57.     public functioninit_args($args
  58.     { 
  59.         $result=array(); 
  60.         for($i=0,$n=count($args);$i<$n;$i++){ 
  61.             $result=self::associative_push($args[$i],$result); 
  62.         } 
  63.         return$result
  64.     } 
  65.  
  66.     public functionassociative_push($arr,$tmp
  67.     { 
  68.         if(is_array($tmp)){ 
  69.             foreach($tmpas$key=>$value){ 
  70.                 $arr[$key]=$value
  71.             } 
  72.             return$arr
  73.         } 
  74.         returnfalse; 
  75.     } 
  76.  
  77.     public functionGetIP() 
  78.     { 
  79.         if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']&&strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'],'unknown')){ 
  80.             return$_SERVER['HTTP_X_FORWARDED_FOR']; 
  81.         }elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){ 
  82.             return$_SERVER['REMOTE_ADDR']; 
  83.         } 
  84.     } 
  85.  
  86.     private functionipCURL($url
  87.     { 
  88.         $ch=curl_init(); 
  89.         curl_setopt($ch,CURLOPT_URL,$url); 
  90.         curl_setopt($ch,CURLOPT_HEADER,0); 
  91.         ob_start(); 
  92.         curl_exec($ch); 
  93.         curl_close($ch); 
  94.         $result=ob_get_contents(); 
  95.         ob_end_clean(); 
  96.         return$result
  97.     } 
  98.  
  99. ?> 
  100. <!DOCTYPE html> 
  101. <html> 
  102. <head> 
  103.     <meta charset="utf8"
  104.     <title>demo</title> 
  105.     <script type="text/javascript"
  106.         varip="<?php new IP(array('ONLYIP'=>true, 'ECHO'=>true));?>"
  107.         vardata=<?php newIP(array('FORMAT'=>'JSON','ECHO'=>true));?>; 
  108.         varresult=''
  109.         for(oo indata){ 
  110.             result+=oo+':'+data[oo]+"\n"
  111.         } 
  112.         alert(result+ip); 
  113.     </script> 
  114. </head> 
  115. <body> 
  116. <h1>CODE:</h1> 
  117.  
  118. <h2>GETIP</h2> 
  119.  
  120. <p>'ONLYIP'=>true,'ECHO'=>true</p> 
  121.  
  122. <p><?phpnewIP(array('ONLYIP'=>true,'ECHO'=>true));?></p> 
  123.  
  124. <P>'ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true</P> 
  125.  
  126. <p><?phpnewIP(array('ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true));?></p> 
  127.  
  128. <h2>GET Location</h2> 
  129.  
  130. <p>'ECHO'=>true</p> 
  131.  
  132. <p><?phpnewIP(array('ECHO'=>true));?></p> 
  133.  
  134. <p>'FORMAT'=>'JSON','ECHO'=>true</p> 
  135.  
  136. <p><?php newIP(array('FORMAT'=>'JSON','ECHO'=>true));?></p> 
  137. </body> 
  138. </html>
  139.  



出处:http://www.phpfensi.com/php/20210511/15212.html


相关教程