程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php查詢IP地址歸屬等信息,

php查詢IP地址歸屬等信息,

編輯:關於PHP編程

php查詢IP地址歸屬等信息,


淘寶公司提供了一個很好用的IP地理信息查詢接口。
在這裡:http://ip.taobao.com/
TaobaoIPQuery2這個類將極大的簡化相關的信息查詢。

 

類 TaobaoIPQuery2 文件:

復制代碼
 1 <?php
 2 /* Usage:
 3  * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress');
 4 
 5 http://www.cnblogs.com/roucheng/
 6  */
 7 Class TaobaoIPQuery2{
 8     private static $_requestURL = 'http://ip.taobao.com/service/getIpInfo.php';
 9     public static function getIPInfo($ip){
10         $long = ip2long($ip);
11         if($long === 0){
12             throw new Exception('IP address error', 5);
13         }
14         $ip=long2ip($long);
15         $IPInfo = self::queryIPInfo($ip);
16         return self::parseJSON($IPInfo);
17     }
18     
19     private static function queryIPInfo($ip){
20         $query = http_build_query(array('ip'=>$ip));
21         $ch = curl_init();
22         $options = array(
23             CURLOPT_URL => sprintf('%s?%s', self::$_requestURL, $query),
24             CURLOPT_RETURNTRANSFER => true,
25             CURLOPT_AUTOREFERER => false,
26             CURLOPT_FOLLOWLOCATION => false,
27             CURLOPT_HEADER => false,
28             CURLOPT_TIMEOUT => 3.0,
29         );
30         curl_setopt_array($ch, $options);
31         $content = curl_exec($ch);
32         curl_close($ch);
33         return $content;
34     }
35     
36     private static function parseJSON($json){
37         $O = json_decode ($json, true);
38         if(false === is_null($O)){
39             return $O;
40         }
41         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
42             $errorCode = json_last_error();
43             if(isset(self::$_JSONParseError[$errorCode])){
44                 throw new Exception(self::$_JSONParseError[$errorCode], 5);
45             }
46         }
47         throw new Exception('JSON parse error', 5);
48     }
49     
50     private static $_JSONParseError = array(
51         JSON_ERROR_NONE=>'No error has occurred',   
52         JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded',   
53         JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded',   
54         JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON',   
55         JSON_ERROR_SYNTAX=>'Syntax error',   
56         JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded',
57     );
58 }
復制代碼

 

TaobaoIPQuery2.Class.php:

復制代碼
 1 <?php
 2 Class TaobaoIPQuery2{
 3     private static $_requestURL = 'http://ip.taobao.com/service/getIpInfo.php';
 4     public static function getIPInfo($ip){
 5         $long = ip2long($ip);
 6         if($long === 0){
 7             throw new Exception('IP address error', 5);
 8         }
 9         $ip=long2ip($long);
10         $IPInfo = self::queryIPInfo($ip);
11         return self::parseJSON($IPInfo);
12     }
13     
14     private static function queryIPInfo($ip){
15         $query = http_build_query(array('ip'=>$ip));
16         $ch = curl_init();
17         $options = array(
18             CURLOPT_URL => sprintf('%s?%s', self::$_requestURL, $query),
19             CURLOPT_RETURNTRANSFER => true,
20             CURLOPT_AUTOREFERER => false,
21             CURLOPT_FOLLOWLOCATION => false,
22             CURLOPT_HEADER => false,
23             CURLOPT_TIMEOUT => 3.0,
24         );
25         curl_setopt_array($ch, $options);
26         $content = curl_exec($ch);
27         curl_close($ch);
28         return $content;
29     }
30     
31     private static function parseJSON($json){
32         $O = json_decode ($json, true);
33         if(false === is_null($O)){
34             return $O;
35         }
36         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
37             $errorCode = json_last_error();
38             if(isset(self::$_JSONParseError[$errorCode])){
39                 throw new Exception(self::$_JSONParseError[$errorCode], 5);
40             }
41         }
42         throw new Exception('JSON parse error', 5);
43     }
44     /* http://www.cnblogs.com/roucheng/ */
45     private static $_JSONParseError = array(
46         JSON_ERROR_NONE=>'No error has occurred',   
47         JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded',   
48         JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded',   
49         JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON',   
50         JSON_ERROR_SYNTAX=>'Syntax error',   
51         JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded',
52     );
53 }
復制代碼

 

調用:

$ip = $_SERVER["REMOTE_ADDR"];
$ipquery = new taobaoIPQuery($ip);
$region = $ipquery->get_region();
$country = $ipquery->get_country();
$city = $ipquery->get_city();

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved