一个PHP获取淘宝IP地址查询接口的代码封装函数类 淘宝接口 淘宝IP接口查地址
通过这个函数,可以直接将你要查询的IP发送到淘宝接口进行查询,并返回该IP的地址信息
<?php
function curlip($getip)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'http://ip.taobao.com/service/getIpInfo.php?ip='.$getip);
curl_setopt($curl, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729)');
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_REFERER, 'http://ip.taobao.com');
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ipaddr = curl_exec($curl);
$ipaddr = json_decode($ipaddr);
$country = $ipaddr->data->country;
$area = $ipaddr->data->area;
$region = $ipaddr->data->region;
$city = $ipaddr->data->city;
$isp = $ipaddr->data->isp;
if($isp == $city){$isp='';}
if($city == $region){$city='';}
if($region == $area){$region='';}
if($region == $country){$region='';}
if($area == $country){$area='';}
$ipaddr = $country.$area.$region.$city.$isp;
return $ipaddr;
}
function get_real_ip()
{
foreach (array(
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER)) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
//会过滤掉保留地址和私有地址段的IP,例如 127.0.0.1会被过滤
//也可以修改成正则验证IP
if ((bool) filter_var($ip, FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 |
FILTER_FLAG_NO_PRIV_RANGE |
FILTER_FLAG_NO_RES_RANGE)) {
return $ip;
}
}
}
}
return null;
}
//使用方法
echo "您的IP:".get_real_ip()."(".curlip(get_real_ip()).")";
echo "<br>".curlip('101.22.36.36');
?>
- 最新评论