Bootstrap

阿里云CDN API推送

官方文档链接:https://help.aliyun.com/document_detail/91164.html

PHP代码:

<?php


class AliCdn
{
    private $accessKeyID = '';
    private $accessKeySecret = '';
    
    public function __construct()
    {
        $aliConfig = config('ali_cdn'); //阿里云配置文件
        if (empty($aliConfig)) {
            $res['code'] = 1001;
            $res['message'] = '请配置cdn推送配置!';
            exit($res);
        }
    
        if (empty($aliConfig['secretId'])) {
             $res['code'] = 1002;
             $res['message'] = '请配置secretId!';
             exit($res);
        }
    
        if (empty($aliConfig['secretKey'])) {
             $res['code'] = 1003;
             $res['message'] = '请配置secretKey!';
             exit($res);
        }
        
        $this->accessKeyID = $aliConfig['secretId'];
        $this->accessKeySecret = $aliConfig['secretKey'];
    }
    
    /**
     * 生成签名结果串
     * Author:刘星麟
     * @param array $data
     * @return array
     */
    public function getSignature($data = [])
    {
        date_default_timezone_set("GMT");
        //公共请求参数
        $publicParams = [
            "Format" => "JSON",
            "Version" => "2018-05-10",
            "AccessKeyId" =>$this->accessKeyID,
            "Timestamp" => date('Y-m-d\TH:i:s\Z'),
            "SignatureMethod" => "HMAC-SHA1",
            "SignatureVersion" => "1.0",
            "SignatureNonce" => substr(md5(rand(1, 99999999)), rand(1, 9), 14),
        ];
        $params = array_merge($publicParams, $data);
        $params['Signature'] = $this->sign($params, $this->accessKeySecret);
        return $params;
    }
    
    /**
     * 签名
     * Author:刘星麟
     * @param $params
     * @param $accessSecret
     * @param string $method
     * @return string
     */
    public function sign($params, $accessSecret, $method = "GET")
    {
        ksort($params);
        $stringToSign = strtoupper($method) . '&' . $this->percentEncode('/') . '&';
        $tmp = "";
        foreach ($params as $key => $val) {
            $tmp .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($val);
        }
        $tmp = trim($tmp, '&');
        $stringToSign = $stringToSign . $this->percentEncode($tmp);
        $key = $accessSecret . '&';
        $hmac = hash_hmac("sha1", $stringToSign, $key, true);
        return base64_encode($hmac);
    }
    
    /**
     * 规范化请求字符串
     * Author:刘星麟
     * @param null $value
     * @return mixed|string|string[]
     */
    public function percentEncode($value = null)
    {
        $en = urlencode($value);
        $en = str_replace("+", "%20", $en);
        $en = str_replace("*", "%2A", $en);
        $en = str_replace("%7E", "~", $en);
        return $en;
    }
    
    /**
     * 推送CDN
     * Author:刘星麟
     * @param $url string 刷新URL
     * @param string number $type 刷新的类型。取值:  1:文件刷新  2:目录刷新  3:正则刷新。
     */
    public function refreshCdn($url, $type = 1)
    {
        $typeArr = [
            1 => 'File',
            2 => 'Directory',
            3 => 'Regex'
        ];
        
        $objectType = $typeArr[$type] ?? '';
        if (empty($objectType)) {
            $res['code'] = 1001;
            $res['message'] = 'CDN刷新的类型错误!';
            return $res;
        }
        if (is_array($url)) {
            $objectPath = implode(PHP_EOL, $url);
        } else {
            $objectPath = $url;
        }
        
        $param['Action'] = 'RefreshObjectCaches';
        $param['ObjectPath'] = $objectPath;
        $param['ObjectType'] = $objectType;
        $signatureParams = $this->getSignature($param);
        $url = 'https://cdn.aliyuncs.com/';
        $requestResJson = curl_data($url, $signatureParams, 'GET', 'json');
        $requestResArr = json_decode($requestResJson, true);
        if (!empty($requestResArr['RefreshTaskId'])) {
            $res['code'] = 200;
            $res['message'] = 'URL刷新成功!';
        } else {
            $res['code'] = 1010;
            $res['message'] = 'URL刷新失败:' . $requestResArr['Code'] ?? '' ;
        }
        
        return $res;
    }
}

?>


生成签名要注意 Timestamp:时区问题  一定要使用GMT

GMT 是时区timezone序列中唯一不受夏令时/冬令时/日间灯节约时间调整的时区。与之对应的是 UTC,但UTC不是时区timezone的概念

;