点击登录
  • 静博客

阿里云API请求类

源码分享 道木樱花 46次浏览 0个评论

很多人在调用阿里云API接口时都应该遇到问题了,所以我就写了一个类供大家用。

<?php
/*阿里云公共API类*/
Class Aliyun{

    private $AccessKeyId; //AccessKey ID

    private $AccessKeySecret; //AccessKey Secret

    private $Format; //返回类型 XML JSON

    private $SignatureMethod; //签名方法,HMAC-SHA1

    private $SignatureVersion; //签名算法版本

    public function __construct($AccessKeyId,$AccessKeySecret,$Format,$SignatureMethod,$SignatureVersion){
        
        $this->AccessKeyId=$AccessKeyId;
        
        $this->AccessKeySecret=$AccessKeySecret;
        
        $this->Format=$Format;
        
        $this->SignatureMethod=$SignatureMethod;
        
        $this->SignatureVersion=$SignatureVersion;
    
    }
    /*
    ** 获取签名后的请求地址
    ** $Action  执行的方法
    ** $Version API版本号,为日期形式:YYYY-MM-DD
    ** $Url     请求的Url
    ** $data    其他请求参数
    */
    public function GetSignature_Url($Action,$Version,$Url,$data=array()){
        
        $publicParams = array(
        
            "Action" => $Action,
        
            "Format" => $this->Format,
        
            "Version" => $Version,
        
            "AccessKeyId" => $this->AccessKeyId,
        
            "Timestamp" => date('Y-m-d\TH:i:s\Z',time()-date('Z')),
        
            "SignatureMethod" => $this->SignatureMethod,
        
            "SignatureVersion" => $this->SignatureVersion,
        
            "SignatureNonce" => substr(md5(rand(1,99999999)),rand(1,9),14),
        
        );
        
        $params = array_merge($publicParams,$data);
        
        $params['Signature'] = $this->Signature($params,$this->AccessKeySecret);
        
        $uri = http_build_query($params);
        
        $url = $Url.'?'.$uri;
        
        return $url;
    
    }
    
    private function Signature($params, $accessSecret, $method = "GET"){
    
        ksort($params);
    
        $stringToSign = strtoupper($method) . '&' . $this->Encode('/') . '&';
    
        $tmp = "";
    
        foreach ($params as $key => $val) {
    
            $tmp .= '&' . $this->Encode($key) . '=' . $this->Encode($val);
    
        }
    
        $tmp = trim($tmp, '&');
    
        $stringToSign = $stringToSign . $this->Encode($tmp);
    
        $key = $accessSecret . '&';
    
        $hmac = hash_hmac("sha1", $stringToSign, $key, true);
    
        return base64_encode($hmac);
    
    }
    
    private function Encode($value = null){
    
        $en = urlencode($value);

        $en = str_replace("+", "%20", $en);
    
        $en = str_replace("*", "%2A", $en);
    
        $en = str_replace("%7E", "~", $en);
    
        return $en;
    
    }

}
//例:查询自己账户下的域名列表
$DomainList=array(

    "PageNum"=>1,
    
    "PageSize"=>5,

);

$AC=new Aliyun('你的AccessKeyId','你的AccessKeySecret','JSON','HMAC-SHA1','1.0');

echo $AC->GetSignature_Url('QueryDomainList','2018-01-29','https://domain.aliyuncs.com/',$DomainList);

 


静博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明阿里云API请求类
喜欢 (0)
[1158230257@qq.com]
分享 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址