PHP query IP address ownership and other information, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:52
Original
945 people have browsed it

php queries IP address ownership and other information,

Taobao provides a very useful IP geographical information query interface.
Here: http://ip.taobao.com/
TaobaoIPQuery2 This class will greatly simplify related information query.

Class TaobaoIPQuery2 File:

PHP query IP address ownership and other information, _PHP tutorial
 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 }
Copy after login
PHP query IP address ownership and other information, _PHP tutorial

TaobaoIPQuery2.Class.php:

PHP query IP address ownership and other information, _PHP tutorial
 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 }
Copy after login
PHP query IP address ownership and other information, _PHP tutorial

Call:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084386.htmlTechArticlephp can query IP address ownership and other information. Taobao provides a very useful IP geographical information query interface. Here: http://ip.taobao.com/ TaobaoIPQuery2 This class will greatly simplify related...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template