php composer 更新使用 阿里云短信

项目文件夹下执行安装Alibaba Cloud SDK for PHP(https://helpcdn.aliyun.com/document_detail/123461.html):
composer require alibabacloud/sdk

 安装完后执行composer dump-autoload

 

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;
        }
    }
}

 

posted on 2021-02-05 15:26  yipianchuyun  阅读(250)  评论(0编辑  收藏  举报

导航