欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

淘宝IP地址查询接口

程序员文章站 2022-05-15 15:06:16
...

淘宝公司提供了一个很好用的IP地理信息查询接口。 在这里:http://ip.taobao.com/ TaobaoIPQuery2这个类将极大的简化相关的信息查询。 无 ?php/* Usage: * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress'); */Class TaobaoIPQuery2{private static $_reques

淘宝公司提供了一个很好用的IP地理信息查询接口。
在这里:http://ip.taobao.com/
TaobaoIPQuery2这个类将极大的简化相关的信息查询。
$ip));
		$ch = curl_init();
		$options = array(
			CURLOPT_URL => sprintf('%s?%s', self::$_requestURL, $query),
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_AUTOREFERER => false,
			CURLOPT_FOLLOWLOCATION => false,
			CURLOPT_HEADER => false,
			CURLOPT_TIMEOUT => 3.0,
		);
		curl_setopt_array($ch, $options);
		$content = curl_exec($ch);
		curl_close($ch);
		return $content;
	}
	
	private static function parseJSON($json){
		$O = json_decode ($json, true);
		if(false === is_null($O)){
			return $O;
		}
		if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
			$errorCode = json_last_error();
			if(isset(self::$_JSONParseError[$errorCode])){
				throw new Exception(self::$_JSONParseError[$errorCode], 5);
			}
		}
		throw new Exception('JSON parse error', 5);
	}
	
	private static $_JSONParseError = array(
		JSON_ERROR_NONE=>'No error has occurred',   
		JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded',   
		JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded',   
		JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON',   
		JSON_ERROR_SYNTAX=>'Syntax error',   
		JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded',
	);
}
$ip));
		$ch = curl_init();
		$options = array(
			CURLOPT_URL => sprintf('%s?%s', self::$_requestURL, $query),
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_AUTOREFERER => false,
			CURLOPT_FOLLOWLOCATION => false,
			CURLOPT_HEADER => false,
			CURLOPT_TIMEOUT => 3.0,
		);
		curl_setopt_array($ch, $options);
		$content = curl_exec($ch);
		curl_close($ch);
		return $content;
	}
	
	private static function parseJSON($json){
		$O = json_decode ($json, true);
		if(false === is_null($O)){
			return $O;
		}
		if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
			$errorCode = json_last_error();
			if(isset(self::$_JSONParseError[$errorCode])){
				throw new Exception(self::$_JSONParseError[$errorCode], 5);
			}
		}
		throw new Exception('JSON parse error', 5);
	}
	
	private static $_JSONParseError = array(
		JSON_ERROR_NONE=>'No error has occurred',   
		JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded',   
		JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded',   
		JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON',   
		JSON_ERROR_SYNTAX=>'Syntax error',   
		JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded',
	);
}