淘宝ip地址查询 淘宝ip地址查询类分享(利用淘宝ip库)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

淘宝ip地址查询 淘宝ip地址查询类分享(利用淘宝ip库)

  2021-03-19 我要评论
想了解淘宝ip地址查询类分享(利用淘宝ip库)的相关内容吗,在本文为您仔细讲解淘宝ip地址查询的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:淘宝ip地址,淘宝ip库,下面大家一起来学习吧。

淘宝公司提供了一个很好用的IP地理信息查询接口。在这里:http://ip.taobao.com/

以下这个taobaoIPQuery类将极大的简化相关的信息查询。

复制代码 代码如下:

<?php

class taobaoIPQuery {

    private $m_ip;
    private $m_content;

    public function __construct($ip) {
        if (isset($ip)) {
            $this->m_ip = $ip;
        } else {
            $this->m_ip = "";
        }
        if (!empty($this->m_ip)) {
            $url_handle = curl_init();
            curl_setopt($url_handle, CURLOPT_URL, "http://ip.taobao.com/service/getIpInfo.php?ip=" . $this->m_ip);
            curl_setopt($url_handle, CURLOPT_RETURNTRANSFER, true);
            $this->m_content = curl_exec($url_handle);
            curl_close($url_handle);
            if ($this->m_content) {
                $this->m_content = json_decode($this->m_content);
                if ($this->m_content->{'code'} == 1) {
                    exit("query error!");
                }
            } else {
                exit("curl error!");
            }
        } else {
            exit("ip address must be not empty!");
        }
    }

    public function get_region() {
        return $this->m_content->{'data'}->{'region'};
    }

    public function get_isp() {
        return $this->m_content->{'data'}->{'isp'};
    }

    public function get_country() {
        return $this->m_content->{'data'}->{'country'};
    }

    public function get_city() {
        return $this->m_content->{'data'}->{'city'};
    }

}

调用很简单

复制代码 代码如下:

$ip = $_SERVER["REMOTE_ADDR"];
$ipquery = new taobaoIPQuery($ip);
$region = $ipquery->get_region();
$country = $ipquery->get_country();
$city = $ipquery->get_city();

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们