tp5 +阿里云短信

it2023-01-19  52

一、引入

composer require alibabacloud/sdk

二、使用

use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; class SendSms { /** * 发送短信验证码 */ //config要传入accessKeyId和accessSecret和SignName和TemplateCode public function sendCode($config,$phone,$code) { //这里传验证码,注意,此处帮我们组成数组了,所以,只穿码就行 $param = [ 'code' => $code ]; //accessKeyId和accessSecret是阿里云上创建子账户 //regionId是地区编号,如值:cn-hangzhou 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(); return $result->toArray(); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } } }
最新回复(0)