>  기사  >  php教程  >  Taobao IP 주소 쿼리 인터페이스

Taobao IP 주소 쿼리 인터페이스

WBOY
WBOY원래의
2016-08-04 08:54:542387검색

Taobao는 매우 유용한 IP 지리 정보 쿼리 인터페이스를 제공합니다. 여기: http://ip.taobao.com/ TaobaoIPQuery2 이 클래스는 관련 정보 쿼리를 크게 단순화합니다. 없음 ?php/* 사용법: * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress') */Class TaobaoIPQuery2{private static $_reques

Taobao는 매우 유용한 IP 지리 정보 쿼리 인터페이스를 제공합니다.
여기: http://ip.taobao.com/
TaobaoIPQuery2 이 클래스는 관련 정보 쿼리를 크게 단순화합니다. 1ac5ad9952dc79489dc410dee5818282
<?php
/* Usage:
 * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress');
 */
Class TaobaoIPQuery2{
	private static $_requestURL = 'http://ip.taobao.com/service/getIpInfo.php';
	public static function getIPInfo($ip){
		$long = ip2long($ip);
		if($long === 0){
			throw new Exception('IP address error', 5);
		}
		$ip=long2ip($long);
		$IPInfo = self::queryIPInfo($ip);
		return self::parseJSON($IPInfo);
	}
	
	private static function queryIPInfo($ip){
		$query = http_build_query(array('ip'=>$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',
	);
}
<?php
Class TaobaoIPQuery2{
	private static $_requestURL = 'http://ip.taobao.com/service/getIpInfo.php';
	public static function getIPInfo($ip){
		$long = ip2long($ip);
		if($long === 0){
			throw new Exception('IP address error', 5);
		}
		$ip=long2ip($long);
		$IPInfo = self::queryIPInfo($ip);
		return self::parseJSON($IPInfo);
	}
	
	private static function queryIPInfo($ip){
		$query = http_build_query(array('ip'=>$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',
	);
}
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 베이스 보드다음 기사:PHP 베이스 보드