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