<?php
/*
 * @Author: Qicloud
 * @Date: 2020-12-05 22:03:33
 * @Title: API
 * @Project: 获取QQ头像外链的加密URL
 * @QQ: 66547997
 * @当浑浊变成一种常态,清白就是一种罪。
 */
class Api
{
    public static function get_curl($url,$header = 0, $ua = 0, $nobaody = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $clwl[] = "Accept:*/*";
        $clwl[] = "Accept-Encoding:gzip,deflate,sdch";
        $clwl[] = "Accept-Language:zh-CN,zh;q=0.8";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $clwl);
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
            //主要头部
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟随重定向
        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    /**
     * @param Json信息输出
     * @arr:需要转换的数组
     */
    public static function json($arr = array(), $code = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
    {
        header("Content-Type:text/json");
        header("Access-Control-Allow-Origin: *");
        exit(json_encode($arr, $code));
    }
    /**
     * 获取GET POST表单数据
     */
    public static function GetData($key = "")
    {
        if (empty($key)) {
            return $_REQUEST;
        }
        if (isset($_REQUEST[$key])) {
            return $_REQUEST[$key];
        } else {
            return null;
        }
    }
}
$qquin = Api::GetData('qq'); //qq参数
if (!empty($qquin)) {
    $size = Api::GetData('size');
    if ($size == 640) {
        $add = Api::get_curl('https://ptlogin2.qq.com/getface?uin=' . $qquin . '&imgtype=3');
        $preg = '#https://.*s=100#'; //使用正则匹配图像URL
        preg_match($preg, $add, $match);
        //var_dump($match);
        $imgurl = $match['0'];
        $deimg = str_replace("s=100", "s=640", $imgurl); //替换图片尺寸
        $qrand = 'q' . rand(1, 4);
        $img = str_replace("thirdqq", $qrand, "$deimg"); //替换域名前缀
        Api::json(['code' => 200, 'msg' => 'success', 'imgurl' => $img]);
    } else {
        $add = Api::get_curl('https://ptlogin2.qq.com/getface?uin=' . $qquin . '&imgtype=3');
        $preg = '#https://.*s=100#'; //使用正则匹配图像URL
        preg_match($preg, $add, $match);
        //var_dump($match);
        $imgurl = $match['0'];
        $qrand = 'q' . rand(1, 4);
        $img = str_replace("thirdqq", $qrand, $imgurl); //替换域名前缀
        Api::json(['code' => 200, 'msg' => 'success', 'imgurl' => $img]);
    }
} else {
    Api::json(['code' => -1, 'msg' => 'errors', 'imgurl' => null]);
}