阿里云短信整合封装类库

原来的整合的阿里云短信每次写新项目的时候还需要重新下载或者composer 

每次都加载感觉很烦

原来自己超级low 整理  阿里云短信整合(新版)https://www.cnblogs.com/YFYQ/p/11543969.html

刚好,我童姐说她整合封装了一份新的阿里云短信

然后,就没有然后了,这还能说什么,趁着刚好有点时间,赶紧借鉴了一下

在借鉴着阿里云的文档,重新整理出来这份类库

首先不用多说,在配置文件config.php 中将添加我们的阿里云短信配置项

    'alisms' => [
        'accessKeyId'  => '#####',  #标识用户
        'accessSecret' => '#####',  #验证用户秘钥
        'SignName'     => '#####',  #短信签名名称
        'TemplateCode' => '#####',  #短信模板ID
    ],

然后封装好对应的类库

如下:

class Smsdemo
{
    private $accessKeyId = "";   #标识用户
    private $accessSecret = "";  #验证用户秘钥
    private $SignName = "";      #短信签名名称  
    private $TemplateCode = "";  #短信模板ID

    public function __construct($accessKeyId,$accessSecret,$SignName,$TemplateCode)
    {
        $this->accessKeyId  = $accessKeyId;
        $this->accessSecret = $accessSecret;
        $this->SignName = $SignName;
        $this->TemplateCode = $TemplateCode;
    }
    /**
     * 发送短信接口
     * @param  [type] $mobile [description]
     * @param  [type] $code   [description]
     * @return [type]         [description]
     * 系统参数
     * SignatureMethod    签名方式。取值范围:HMAC-SHA1。
     * SignatureNonce     签名唯一随机数
     * AccessKeyId        访问秘钥ID
     * SignatureVersion   签名算法版本 1.0
     * Timestamp          请求的时间戳
     * Format             返回的语言类型
     * 
     * 业务API参数
     * Action             请求的API方法名称
     * Version            API 的版本号  2017-05-25
     * RegionId           API支持的RegionID  cn-hangzhou
     *
     * PhoneNumbers       接收短信的手机号码
     * SignName           短信签名名称 
     * TemplateParam      短信模板变量对应的实际值,JSON格式
     * TemplateCode       短信模板ID
     */
    public function  sendsms($mobile,$code)
    {
        $Timestamp = gmdate("Y-m-d\TH:i:s\Z");

        #系统参数
        $sysdata = [
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => $this->uuid(),
            'AccessKeyId' => $this->accessKeyId,
            'SignatureVersion' => "1.0",
            'Timestamp' => $Timestamp,
            'Format' => 'json',
        ];

        #业务API参数
        $apidata = $this->apidata($mobile,$code);

        #合并参数
        $datas = array_merge($sysdata,$apidata);

        #升序排列
        ksort($datas);

        #拼接加密字符串
        $sortedQueryStringTmp  = "";
        foreach($datas as $key=>$val)
        {
            $sortedQueryStringTmp  .= "&".$key."=".$this->encode($val);
        }
        $stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));

        #加密转码 
        $sign = base64_encode(hash_hmac("sha1", $stringToSign, $this->accessSecret . "&",true));

        #生成签名
        $signature = $this->encode($sign);

        #拼接请求链接
        $api = 'dysmsapi.aliyuncs.com';
        $url = "https://".$api."/?Signature=".$signature.$sortedQueryStringTmp;

        try {
            $content = $this->curlRuqust($url);
            return json_decode($content,true);
        } catch( \Exception $e) {
            return false;
        }
    }

    /**
     * 业务参数整合
     * @param  [type] $mobile [description]
     * @param  [type] $code   [description]
     * @return [type]         [description]
     */
    public function apidata($mobile,$code)
    {
        $apidata = [
            'Action' =>  'SendSms',
            'Version' => '2017-05-25',
            'RegionId' => 'cn-hangzhou',
            'PhoneNumbers' => $mobile,
            'SignName' => $this->SignName,
            'TemplateParam' => "{\"code\":\"".$code."\"}",
            'TemplateCode' => $this->TemplateCode,
        ];
        return $apidata;
    }

    /**
     * 阿里云短信对应转码替换
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    private function encode($str)
    {
        $res = urlencode($str);
        $res = preg_replace("/\+/", "%20", $res);
        $res = preg_replace("/\*/", "%2A", $res);
        $res = preg_replace("/%7E/", "~", $res);
        return $res;
    }

    /**
     * PHP生成 UUID
     * @return [type] [description]
     */
    public function  uuid()  
    {  
        $chars = md5(uniqid(mt_rand(), true));  
        $uuid = substr ( $chars, 0, 8 ) . '-'
                . substr ( $chars, 8, 4 ) . '-' 
                . substr ( $chars, 12, 4 ) . '-'
                . substr ( $chars, 16, 4 ) . '-'
                . substr ( $chars, 20, 12 );  
        return $uuid ;  
    }  

    /**
     * 对应CURL请求
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    private function curlRuqust($url) 
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0"
        ));
        if(substr($url, 0,5) == 'https') 
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $rtn = curl_exec($ch);
        if($rtn === false) 
        {
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch); 
        return $rtn;
    }

}

 

这样就完成咯

然后调用:

        $mobile = "#######";   #发送手机号号码
        $code = "#####";  #对应验证码

        $alisms = new Smsdemo(config('alisms.accessKeyId'),config('alisms.accessSecret'),config('alisms.SignName'),config('alisms.TemplateCode'));
        $res = $alisms->sendsms($mobile,$code);

        if($res['Code'] == 'OK')
        {
            return '发送成功';
        }

 

返回结果数据

 

 OK 

完成

标注下我童姐封装的博客

坐标:思君邪 

封装类库:  https://www.cnblogs.com/exo5/p/13572490.html

她的短信整合类库:https://www.cnblogs.com/exo5/p/13576063.html

 

人生总是要学会接受,接受自己不能所接受的。。。

2020年09月08日

 

posted @ 2020-09-08 10:55  御风琊穹  阅读(508)  评论(0编辑  收藏  举报

春风十里,我喜欢你。可是你喜欢的人不喜欢你,哈哈....
人来人往,莫失莫忘。最终还是选择淡忘于回忆之中...
落日余晖,待你而归。但你终究不是为我而归..
一米阳光,温暖安放;心若向阳,无谓悲伤;轻安喜乐,次第花开 。