时间:2021-07-01 10:21:17 帮助过:14人阅读
淘宝公司提供了一个很好用的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();
http://www.bkjia.com/PHPjc/1084386.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1084386.htmlTechArticlephp查询IP地址归属等信息, 淘宝公司提供了一个很好用的IP地理信息查询接口。 在这里:http://ip.taobao.com/ TaobaoIPQuery2这个类将极大的简化相关...