OnlineService.php 6.7 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

李光春's avatar
李光春 已提交
17
namespace DtApp\ThinkLibrary\service\Ip;
李光春's avatar
李光春 已提交
18

李光春's avatar
李光春 已提交
19
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
20
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
21
use DtApp\ThinkLibrary\service\curl\HttpService;
李光春's avatar
李光春 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * IP  - 在线查询接口
 * Class OnlineService
 * @package DtApp\ThinkLibrary\service\ip
 */
class OnlineService extends Service
{
    /**
     * 需要查询的IP
     * @var
     */
    private $ip;

    /**
     * 查询指定IP
     * @param string $str
     * @return $this
     */
李光春's avatar
李光春 已提交
41
    public function ip(string $str): self
李光春's avatar
李光春 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    {
        $this->ip = $str;
        return $this;
    }

    /**
     * 哔哩哔哩ip查询接口
     * @return array|bool|mixed|string
     */
    public function biliBili()
    {
        $url = "https://api.bilibili.com/x/web-interface/zone";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * batch
     * @param string $lang 语言
     * @return array|bool|mixed|string
     */
    public function batch(string $lang = 'zh-CN')
    {
        $url = "http://ip-api.com/json/?lang={$lang}";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * lookup
     * @return array|bool|mixed|string
     */
    public function lookup()
    {
        $url = "https://extreme-ip-lookup.com/json/";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * 网易IP查询接口
     * @return array|bool|mixed|string
     */
    public function netEase()
    {
        $url = "https://ipservice.3g.163.com/ip";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * 百度搜索
     * @return bool|false|mixed|string|string[]
     */
    public function baidu()
    {
李光春's avatar
李光春 已提交
102 103 104
        if (empty($this->ip)) {
            $this->ip = get_ip();
        }
李光春's avatar
李光春 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$this->ip}&co=&resource_id=6006&ie=utf8&oe=utf8&cb=json";
        $res = HttpService::instance()
            ->url($url)
            ->toArray(false);
        $res = str_replace("/**/json", "", $res);
        $res = substr($res, 1);
        $res = substr($res, 0, -2);
        $res = json_decode($res, true);
        return $res;
    }

    /**
     * 太平洋
     * @return bool|false|mixed|string
     */
李光春's avatar
李光春 已提交
120
    public function pConLine()
李光春's avatar
李光春 已提交
121 122
    {
        $url = "http://whois.pconline.com.cn/ipJson.jsp?json=true";
李光春's avatar
李光春 已提交
123 124 125
        if (!empty($this->ip)) {
            $url = "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip={$this->ip}";
        }
李光春's avatar
李光春 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        $res = HttpService::instance()
            ->url($url)
            ->toArray(false);
        preg_match('/{.+}/', $res, $res);
        $res = iconv('gbk', 'utf-8', $res[0]);
        $res = json_decode($res, true);
        return $res;
    }

    /**
     * 新浪
     * @return bool|false|mixed|string|string[]
     */
    public function siNa()
    {
李光春's avatar
李光春 已提交
141 142 143
        if (empty($this->ip)) {
            $this->ip = get_ip();
        }
李光春's avatar
李光春 已提交
144 145 146 147 148 149 150
        $url = "http://ip.ws.126.net/ipquery?ip={$this->ip}";
        $res = HttpService::instance()
            ->url($url)
            ->toArray(false);
        $res = iconv('gbk', 'utf-8', $res);
        $res = substr($res, strpos($res, "{"));
        $res = substr($res, 0, -2);
李光春's avatar
李光春 已提交
151
        $res = str_replace(array("city", "province"), array('"city"', '"province"'), $res);
李光春's avatar
李光春 已提交
152 153 154 155 156 157 158 159 160 161
        $res = json_decode($res, true);
        return $res;
    }

    /**
     * 好搜
     * @return bool|mixed|string
     */
    public function so()
    {
李光春's avatar
李光春 已提交
162 163 164
        if (empty($this->ip)) {
            $this->ip = get_ip();
        }
李光春's avatar
李光春 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        $url = "https://open.onebox.so.com/dataApi?type=ip&src=onebox&tpl=0&num=1&query=ip&ip={$this->ip}&url=ip";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * 搜狐
     */
    public function soHu()
    {
        $url = "http://pv.sohu.com/cityjson?ie=utf-8";
        $res = HttpService::instance()
            ->url($url)
            ->toArray(false);
        $res = str_replace("var returnCitySN = ", "", $res);
        $res = substr($res, 0, -1);
        $res = json_decode($res, true);
        return $res;
    }
李光春's avatar
李光春 已提交
185 186 187 188 189 190 191 192

    /**
     * 淘宝
     * @param string $ip IP地址
     * @return bool|mixed|string
     */
    public function taoBao(string $ip = '')
    {
李光春's avatar
李光春 已提交
193 194 195
        if (empty($this->ip)) {
            $this->ip = get_ip();
        }
李光春's avatar
李光春 已提交
196 197 198 199 200 201 202 203 204 205
        $url = "http://ip.taobao.com/service/getIpInfo.php?ip={$this->ip}";
        return HttpService::instance()
            ->url($url)
            ->toArray();
    }

    /**
     * 阿里云
     * @param string $appcode
     * @return bool|mixed|string
李光春's avatar
李光春 已提交
206
     * @throws DtaException
李光春's avatar
李光春 已提交
207 208 209
     */
    public function aliYun(string $appcode = '')
    {
李光春's avatar
李光春 已提交
210 211 212
        if (empty($this->ip)) {
            $this->ip = get_ip();
        }
李光春's avatar
李光春 已提交
213 214 215
        $host = "http://iploc.market.alicloudapi.com";
        $path = "/v3/ip";
        $method = "GET";
李光春's avatar
李光春 已提交
216 217 218
        if (empty($appcode)) {
            throw new DtaException('请检查阿里-阿里云配置信息 appcode');
        }
李光春's avatar
李光春 已提交
219
        $headers = array();
李光春's avatar
李光春 已提交
220
        $headers[] = "Authorization:APPCODE " . $appcode;
李光春's avatar
李光春 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        $querys = "ip={$this->ip}";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $content = curl_exec($curl);
        curl_close($curl);
        return json_decode($content, true);
    }
李光春's avatar
李光春 已提交
240
}