700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > PHP阿里云短信服务

PHP阿里云短信服务

时间:2020-05-18 09:46:45

相关推荐

PHP阿里云短信服务

官方

官方地址

/document_detail/55359.html?spm=5176.8195934.507901.12.b1ngGK官方的申请使用例子文章参考

/lw545034502/article/details/80680098

插件

先用composer安装插件包

composer require alibabacloud/client

简单封装一下

<?phpnamespace app\tools;use AlibabaCloud\Client\AlibabaCloud;use AlibabaCloud\Client\Exception\ClientException;use AlibabaCloud\Client\Exception\ServerException;class Alisms{//配置protected $config = [];public function __construct($config){$this->config = $config;}/*** 发送* @param string $phone 接收短信的手机号码* @param string $content 短信内容(json)* @return array* @throws ClientException*/public function send($template, $phone = '', $content = ''){AlibabaCloud::accessKeyClient($this->config['AccessKeyId'], $this->config['accessSecret'])->regionId('cn-hangzhou')->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Dysmsapi')// ->scheme('https') // https | http->version('-05-25')->action('SendSms')->method('POST')->host('')->options(['query' => ['PhoneNumbers' => $phone,'SignName' => $this->config['SignName'],'TemplateCode' => $template,'TemplateParam' => $content,'RegionId' => $this->config['RegionId'],],])->request();if ($result['Code'] == 'OK' || $result['Message'] == 'ok') {return ['code' => 0, 'msg'=>'发送成功'];} else {return ['code' => 4003, 'msg'=>$result['Message']];}} catch (ClientException $e) {return ['code' => 500, 'msg'=>$e->getErrorMessage()];} catch (ServerException $e) {return ['code' => 500, 'msg'=>$e->getErrorMessage()];}}}

调用

//配置$config = ['AccessKeyId' => '','accessSecret' => '','SignName' => '','RegionId' => ''];//生成验证码$auth_code = rand(1000,9999);//短信模板$template = '';//发送内容,要根据实际的模板内容(验证码)$content = json_encode(['code'=>$auth_code]);$sms = new Alisms($config);$result = $sms->send($template, $phone, $content);if ($result['code'] != 0) {return api_json_return(500, '发送短信失败,请联系管理员');}return api_json_return(0, '发送成功');

如果您觉得本篇对你有帮助,可以点关注,给个赞,支持一下,过程有遇到什么问题也欢迎评论私信,进行交流

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。