php通過淘寶API查詢IP地址歸屬等信息
淘寶公司提供了一個(gè)很好用的IP地理信息查詢接口。
在這里:http://ip.taobao.com/
TaobaoIPQuery2這個(gè)類將極大的簡(jiǎn)化相關(guān)的信息查詢。
類 TaobaoIPQuery2 文件:
<?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',
);
}
TaobaoIPQuery2.Class.php:
<?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',
);
}
調(diào)用:
$ip = $_SERVER["REMOTE_ADDR"]; $ipquery = new taobaoIPQuery($ip); $region = $ipquery->get_region(); $country = $ipquery->get_country(); $city = $ipquery->get_city();
我們?cè)賮砜磦€(gè)騰訊api接口的
/*
*根據(jù)騰訊IP分享計(jì)劃的地址獲取IP所在地,比較精確
*/
function getIPLoc_QQ($queryIP){
$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_ENCODING ,'gb2312');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數(shù)據(jù)返回
$result = curl_exec($ch);
$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 編碼轉(zhuǎn)換,否則亂碼
curl_close($ch);
preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}
騰訊的IP分享計(jì)劃的查詢接口連接是:http://ip.qq.com/cgi-bin/searchip,該連接后面會(huì)帶有一個(gè)get參數(shù)searchip1,即searchip1=你要查詢的IP地址。采用PHP向http://ip.qq.com/cgi-bin/searchip發(fā)送http get request,然后獲取相應(yīng),獲得相應(yīng)結(jié)果后,將結(jié)果用正則表達(dá)式將需要的地理位置信息提取出來就OK了。當(dāng)然,PHP發(fā)送get請(qǐng)求的方法有很多,我用curl來模擬http請(qǐng)求的方法
Curl是PHP的一個(gè)擴(kuò)展,進(jìn)行測(cè)試之前,先確認(rèn)PHP已經(jīng)加載了curl擴(kuò)展。打開PHP配置文件php.ini,搜索 “extension=php_curl.dll”,將其前面的分號(hào)(;)去掉,然后重啟HTTP服務(wù)器,查看phpinfo(),若能看到curl的信 息,說明PHP擴(kuò)展curl成功
接下來是新浪網(wǎng)的API接口
新浪的IP查詢接口地址是:http://int.dpool.sina.com.cn/iplookup/iplookup.php,該連接后面也帶有g(shù)et參數(shù),參數(shù)ip=你要查詢的IP地址,format=返回的查詢結(jié)果格式。為了方便起見,返回的格式采用json,即format=json,因此,PHP出來擴(kuò)展curl外,還得擴(kuò)展json:
/*
*根據(jù)新浪IP查詢接口獲取IP所在地
*/
function getIPLoc_sina($queryIP){
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryIP;
$ch = curl_init($url);
//curl_setopt($ch,CURLOPT_ENCODING ,'utf8');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數(shù)據(jù)返回
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if($location===FALSE) return "";
if (emptyempty($location->desc)) {
$loc = $location->province.$location->city.$location->district.$location->isp;
}else{
$loc = $location->desc;
}
return $loc;
}
使用getIPLoc_sina("183.37.209.57")即可得到IP地址所在的地址位置。
相關(guān)文章
php實(shí)現(xiàn)的簡(jiǎn)單美國商品稅計(jì)算函數(shù)
這篇文章主要介紹了php實(shí)現(xiàn)的簡(jiǎn)單美國商品稅計(jì)算函數(shù),涉及php數(shù)學(xué)計(jì)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Server.HTMLEncode讓代碼在頁面里顯示為源代碼
這篇文章為大家介紹的是把寫入數(shù)據(jù)庫的html源代碼以html源編碼的形式顯示在頁面里面,這個(gè)是asp中的函數(shù)2013-12-12
PHP常用操作類之通信數(shù)據(jù)封裝類的實(shí)現(xiàn)
PHP類的封裝對(duì)于初學(xué)者來說是比較難懂的,下面這篇文章主要給大家介紹了關(guān)于PHP常用操作類之通信數(shù)據(jù)封裝類實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
PHP實(shí)現(xiàn)模擬http請(qǐng)求的方法分析
這篇文章主要介紹了PHP實(shí)現(xiàn)模擬http請(qǐng)求的方法,簡(jiǎn)單分析了http請(qǐng)求的原理、流程及php實(shí)現(xiàn)模擬http請(qǐng)求的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
php sprintf()函數(shù)讓你的sql操作更安全
本函數(shù)用來將字符串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號(hào) % 開始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了2008-07-07
PHP回調(diào)函數(shù)概念與用法實(shí)例分析
這篇文章主要介紹了PHP回調(diào)函數(shù)概念與用法,簡(jiǎn)單介紹了回調(diào)函數(shù)的概念、原理,并結(jié)合實(shí)例形式分析了回調(diào)函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2017-11-11
Php獲取金書網(wǎng)的書名的實(shí)現(xiàn)代碼
php獲取金書網(wǎng)的書名的實(shí)現(xiàn)代碼。2010-06-06
解析php函數(shù)method_exists()與is_callable()的區(qū)別
本篇文章是對(duì)php中method_exists()與is_callable()的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

