在写这篇文章的时候。阿里云的全站加速api还没有完成,SDK还没有写好。

文档是这B样

图

下面的签名和请求部分代码

<?php
/**
 * Created by PhpStorm.
 * User: qidong
 * Date: 2019-02-18
 * Time: 15:30
 */

namespace Home\Service;


class AliCdn
{

    public $aliUrl="http://dcdn.aliyuncs.com";
    public $data;
    private $accessKeyId = "  ";
    private $accessKeySecret = " ";
    public $url;

    public function __construct(){
        date_default_timezone_set("GMT");
        $this->data = [
                'Format'=>'JSON',
                'Version'=>'2018-01-15',
                'SignatureMethod'=>'HMAC-SHA1',
                'SignatureNonce'=>rand(1111111111,9999999999),
                'SignatureVersion'=>'1.0',
                'AccessKeyId'=>$this->accessKeyId,
                'Timestamp'=>date('Y-m-d').'T'.date('H:i:s').'Z',
            ];
        date_default_timezone_set ('PRC');
    }



    /**
     * 预热cdn的缓存。主动把缓存部署到节点上
     * @param string $ObjectPath
     * @return array
     */
    public function PreloadDcdnObjectCaches($ObjectPath="www.bjpowernode.com/zt/ten")
    {
        $param['Action']="PreloadDcdnObjectCaches";
        $param['ObjectPath']=$ObjectPath;
        $this->data=array_merge($this->data,$param);
        $this->data['Signature']=$this->computeSignature($this->data);

        $res=HttpClient::get($this->aliUrl."?".http_build_query($this->data));
        $resArr=(array)json_decode($res,true);
        $resArr['status']=0;
        if(array_key_exists('PushTaskId',$resArr)){
            $resArr['status']=1;
        }
        return $resArr;

    }


    /**
     * 刷新url地址缓存、指定File或Directory
     * @param string $ObjectPath
     * @param string $ObjectType
     * @return array
     */
    public function RefreshDcdnObjectCaches($ObjectPath="",$ObjectType="File")
    {
        if($ObjectType=="Directory"){$ObjectPath=rtrim($ObjectPath,'/')."/";}
        $param['Action']="RefreshDcdnObjectCaches";
        $param['ObjectPath']=$ObjectPath;
        $param['ObjectType']=$ObjectType;
        $this->data=array_merge($this->data,$param);
        $this->data['Signature']=$this->computeSignature($this->data);
        $res=HttpClient::get($this->aliUrl."?".http_build_query($this->data));
        $resArr=(array)json_decode($res,true);
        $resArr['status']=0;
        if(array_key_exists('RefreshTaskId',$resArr)){
            $resArr['status']=1;
        }
        return $resArr;

    }


    protected function percentEncode($str)
    {
        // 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        $res = preg_replace('/%7E/', '~', $res);
        return $res;
    }

    protected function computeSignature($parameters)
    {
        // 将参数Key按字典顺序排序
        ksort($parameters);
        // 生成规范化请求字符串
        $canonicalizedQueryString = '';
        foreach($parameters as $key => $value)
        {
            $canonicalizedQueryString .= '&amp;' . $this->percentEncode($key)
                . '=' . $this->percentEncode($value);
        }
        // 生成用于计算签名的字符串 stringToSign
        $stringToSign = 'GET&amp;%2F&amp;' . $this->percentencode(substr($canonicalizedQueryString, 1));
        // 计算签名,注意accessKeySecret后面要加上字符'&amp;'
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&amp;', true));
        return $signature;
    }



}
Last modification:January 31, 2020
如果觉得我的文章对你有用,请随意赞赏