thinkphp6阿里云短信单文件自定义封装类

不使用阿里云提供的SDK

lib/Dysms.php文件

<?php
namespace lib;

class Dysms
{
    // Access Key ID
    private $accessKeyId = '';
    // Access Key Secret
    private $accessKeySecret = '';
    // 短信签名
    private $signName = '';
    // 模版ID
    private $templateCode = '';
    // 服务器地址
    private $host = 'https://dysmsapi.aliyuncs.com/';
    /**
     * 构造
     */
    public function __construct($config = array())
    {
        //$config = array(
        //    'access_key' => '***********************',
        //    'access_secret' => '***********************',
        //    'sign_name' => '短信签名',
        //    'template_code' => 'SMS_111111111',
        //);
        $this->accessKeyId = isset($config['access_key']) ? $config['access_key'] : '';
        $this->accessKeySecret = isset($config['access_secret']) ? $config['access_secret'] : '';
        $this->signName = isset($config['sign_name']) ? $config['sign_name'] : '';
        $this->templateCode = isset($config['template_code']) ? $config['template_code'] : '';
    }
    /**
     * 发送验证码
     */
    public function send($mobile, $code)
    {
        $params = array(
            'SignName' => $this->signName,
            'Format' => 'JSON',
            'Version' => '2017-05-25',
            'AccessKeyId' => $this->accessKeyId,
            'SignatureVersion' => '1.0',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => uniqid(),
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Action' => 'SendSms',
            'TemplateCode' => $this->templateCode,
            'PhoneNumbers' => $mobile,
            'TemplateParam' => '{"code":"' . $code . '"}',
        );
        $params['Signature'] = $this->make($params);
        $url = $this->host . '?' . http_build_query($params);
        $result = json_decode($this->get($url), true);
        if (!isset($result['Code'])) {
            return array(
                'errcode' => 3001,
                'errmsg' => '远程服务器无应答',
            );
        }
        if ('OK' !== $result['Code']) {
            return array(
                'errcode' => $result['Code'],
                'errmsg' => $result['Message'],
            );
        }
        return array(
            'errcode' => 0,
            'errmsg' => 'success',
        );
    }
    /**
     * 解码
     */
    private function denc($str)
    {
        $str = urlencode($str);
        $str = preg_replace('/\+/', '%20', $str);
        $str = preg_replace('/\*/', '%2A', $str);
        $str = preg_replace('/%7E/', '~', $str);
        return $str;
    }
    /**
     * 签名
     */
    private function make($params)
    {
        ksort($params);
        $query = '';
        foreach ($params as $key => $value) {
            $query .= '&' . $this->denc($key) . '=' . $this->denc($value);
        }
        $string = 'GET&%2F&' . $this->denc(substr($query, 1));
        $signature = base64_encode(hash_hmac('sha1', $string, $this->accessKeySecret . '&', true));
        return $signature;
    }
    /**
     * http发送get请求
     */
    private function get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $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_HEADER, 0);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }
}

控制器中调用

use lib\Dysms;

$mobile = '13000000001';
$code = '123321';
$sms = new Dysms(array(
    'access_key' => '********',
    'access_secret' => '********',
    'sign_name' => '短信签名',
    'template_code' => '短信模板ID',
));
$result = $sms->send($mobile, $code);
print_r($result);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xmode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值