拒绝冗余繁琐,thinkphp6阿里云短信单文件自定义封装类

2024年03月23日 PHP手机验证码 阿里云手机验证码 PHP阿里云短信验证 PHP短信验证 懒猪技术

前两天有一客户需要会员手机验证码登录,然后客户指定要用阿里云,然后我用composer安装阿里云短信插件包,发现太繁琐了,突然又想起以前见过的单文件封装类,这下省事多了,既完美解决又不臃肿,下面直接上代码,方便自己以后再次使用

<?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);

本文链接:http://so.lmcjl.com/news/200/

展开阅读全文
相关内容