1.登陆阿里云后台,事先添加签名和模板

 

2.使用composer下载阿里云SDK

composer require alibabacloud/sdk

在PHP7.0下安装需要提前安装curl扩展

sudo apt-get install php7.0-curl

 

3. 提取短信发送配置到配置文件

return [
    \'accessKeyId\' => \'LTAIMje******hS\',
    \'accessSecret\' => \'fMuQTLUrKQN******Z2m07\',
    \'SignName\' => \'****\',
    \'TemplateCode\' => \'SMS_****04\',
    \'regionId\' => \'cn-hangzhou\'
];

 

4. 新建阿里短信发送类

use Swoft\Task\Bean\Annotation\Task;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

/**
 * 阿里信息发送类
 * @Task("ali")
 */
class AliTask
{
    /**
     * 发送短信验证码
     */
    public function sendCode($phone,$code)
    {
        $config = \config(\'ali\');
        $param = [
            \'code\' => $code
        ];
        AlibabaCloud::accessKeyClient($config[\'accessKeyId\'], $config[\'accessSecret\'])
            ->regionId($config[\'regionId\'])
            ->asGlobalClient();

        try {
            $result = AlibabaCloud::rpcRequest()
                ->product(\'Dysmsapi\')
                ->version(\'2017-05-25\')
                ->action(\'SendSms\')
                ->method(\'POST\')
                ->options([
                    \'query\' => [
                        \'PhoneNumbers\' => $phone,
                        \'SignName\' => $config[\'SignName\'],
                        \'TemplateCode\' => $config[\'TemplateCode\'],
                        \'TemplateParam\' => json_encode($param)
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }
}

 

版权声明:本文为xiaoliwang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaoliwang/archive/2004/01/13/10441316.html