php查询IP地址归属等信息,_PHP教程
程序员文章站
2022-06-15 17:30:14
...
php查询IP地址归属等信息,
淘宝公司提供了一个很好用的IP地理信息查询接口。
在这里:http://ip.taobao.com/
TaobaoIPQuery2这个类将极大的简化相关的信息查询。
类 TaobaoIPQuery2 文件:
1 $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 $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();
推荐阅读
-
PHP curl伪造IP地址和header信息代码实例
-
php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
-
php获取客户端IP地址、所在地、操作系统、浏览器信息
-
php通过淘宝API查询IP地址归属等信息
-
PHP获取当前服务器版本,Ip等详细信息
-
php二分法在IP地址查询中的应用
-
php限制ip地址范围的方法_PHP教程
-
php中使用getimagesize获取图片、flash等文件的尺寸信息实例_PHP教程
-
【php】利用新浪api接口与php获取远程数据的步骤,获取IP地址,并获取相应的IP归属地
-
【php】利用新浪api接口与php获取远程数据的方法,获取IP地址,并获取相应的IP归属地