php通过淘宝API查询IP地址归属等信息

这篇文章主要介绍了php通过淘宝API查询IP地址归属等信息的相关资料,需要的朋友可以参考下

/* 
 *根据腾讯IP分享计划的地址获取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) ; // 获取数据返回 
  $result = curl_exec($ch); 
  $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码 
  curl_close($ch); 
  preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray); 
  $loc = $ipArray[1]; 
  return $loc; 
}

http://www.jb51.net/article/76960.htm

/**
 * 通过淘宝IP接口获取IP地理位置
 * @param string $ip
 * @return: string
 **/
function getCity($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);

    $ipinfo=json_decode($file_contents);
    if($ipinfo->code=='1'){
        return false;
    }
    $city = $ipinfo->data->region.$ipinfo->data->city;
    return $city;

}
/* 
 *根据新浪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) ; // 获取数据返回 
  $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; 
} 
function getCity($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ip=json_decode(file_get_contents($url));
    if((string)$ip->code=='1'){
        return false;
    }
    $city = $ip->data->region.$ip->data->city.' ' .$ip->data->isp;
    return $city;
}
echo getCity("221.226.5.1");  
function getCity($ip){  
    //淘宝查询接口  
    $url = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;  
    //用curl发送接收数据  
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_ENCODING, 'utf8');  
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $location = curl_exec($ch);  
    $location = json_decode($location);  
    curl_close($ch);  
    return($location->data->city);  
}  
//stdClass Object ( [code] => 0 [data] => stdClass Object ( [country] => 中国 [country_id] => CN [area] => 华东 [area_id] => 300000 [region] => 江苏省 [region_id] => 320000 [city] => 南京市 [city_id] => 320100 [county] => [county_id] => -1 [isp] => 电信 [isp_id] => 100017 [ip] => 221.226.5.1 ) )   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值