阿里云云解析服务精简版PHP-SDK

5个月前 教程分享 160°C

这篇文章最后更新于08-26 10:53前,您需要注意相关的内容是否还可用,如有疑问请联系作者!

阿里云官方的SDK文件太多,使用不方便,因此我也写了一个精简版的PHP-SDK,只需要一个php文件即可实现。

这个SDK是以阿里云云解析服务为例,其他阿里云服务只需要改一下里面的参数也可以用。

<?php
//云解析服务精简版SDK

class AliyunSimple {
    private $AccessKeyId;
    private $AccessKeySecret;
    private $Endpoint = 'alidns.aliyuncs.com'; //API接入域名
    private $Version = '2015-01-09'; //API版本号

    function __construct($AccessKeyId, $AccessKeySecret){
        $this->AccessKeyId = $AccessKeyId;
        $this->AccessKeySecret = $AccessKeySecret;
    }

    //获取子域名解析记录列表
    public function DescribeSubDomainRecords($SubDomain){
        $param = ['Action' => __FUNCTION__, 'SubDomain' => $SubDomain];
        return $this->request($param, true);
    }

    //添加解析记录
    public function AddDomainRecord($DomainName, $RR, $Type, $Value, $Line = 'default', $TTL = 600){
        $param = ['Action' => __FUNCTION__, 'DomainName' => $DomainName, 'RR' => $RR, 'Type' => $Type, 'Value' => $Value, 'Line' => $Line, 'TTL' => $TTL];
        return $this->request($param);
    }

    //修改解析记录
    public function UpdateDomainRecord($RecordId, $RR, $Type, $Value, $Line = 'default', $TTL = 600){
        $param = ['Action' => __FUNCTION__, 'RecordId' => $RecordId, 'RR' => $RR, 'Type' => $Type, 'Value' => $Value, 'Line' => $Line, 'TTL' => $TTL];
        return $this->request($param);
    }

    //删除解析记录
    public function DeleteDomainRecord($RecordId){
        $param = ['Action' => __FUNCTION__, 'RecordId' => $RecordId];
        return $this->request($param);
    }

    //设置解析记录状态
    public function SetDomainRecordStatus($RecordId, $Status){
        $param = ['Action' => __FUNCTION__, 'RecordId' => $RecordId, 'Status' => $Status];
        return $this->request($param);
    }


    //签名方法
    private function aliyunSignature($parameters, $accessKeySecret, $method)
    {
        ksort($parameters);
        $canonicalizedQueryString = '';
        foreach ($parameters as $key => $value) {
            $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
        }
        $stringToSign = $method . '&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
        $signature = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret."&", true));

        return $signature;
    }
    private function percentEncode($str)
    {
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        $res = preg_replace('/%7E/', '~', $res);
        return $res;
    }
    //请求方法(当需要返回列表等数据时,returnData=true)
    private function request($param, $returnData=false){
        if(empty($this->AccessKeyId)||empty($this->AccessKeySecret))return false;
        $url='https://'.$this->Endpoint.'/';
        $data=array(
            'Format' => 'JSON',
            'Version' => $this->Version,
            'AccessKeyId' => $this->AccessKeyId,
            'SignatureMethod' => 'HMAC-SHA1',
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'SignatureVersion' => '1.0',
            'SignatureNonce' => random(8));
        $data=array_merge($data, $param);
        $data['Signature'] = $this->aliyunSignature($data, $this->AccessKeySecret, 'POST');
        $ch=curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $json=curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $arr=json_decode($json,true);
        if($returnData==true){
            return $arr;
        }else{
            if($httpCode==200){
                return true;
            }else{
                return $arr['Message'];
            }
        }
    }
}

调用方法如下:

$aliyun = new AliyunSimple('阿里云AccessKeyId','阿里云AccessKeySecret');
//获取子域名解析记录列表。这种方法可以自己在类里面增加
$result = $aliyun->DescribeSubDomainRecords($SubDomain);
var_dump($result);

文章来源于:http://blog.cccyun.cn/m/?post=420

版权说明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。
本文链接:https://huue.cc/index.php/archives/1145.html
最后编辑: 21年08月26日

随机内容

网友评论

4天前

大佬啥时候参与max/routing的开发啊,把功能加到routing中 https://github.com/topyao/max-routing

6天前

11111

1个月前

1111

1个月前

1

2个月前

e21e12e12

2个月前

菊花残,满腚伤。ଲ