PHP如何实现阿里云短信sdk灵活应用在项目中的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-25 15:50:08 浏览: 评论:0
这篇文章主要介绍了PHP如何实现阿里云短信sdk灵活应用在项目中的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧。
首先安装
composer require alibabacloud/sdk
接着看alibabacloud官网文档
https://packagist.org/packages/alibabacloud/sdk
示例
My_composer_aliyunsms.php
- <?php
- // use Swoft\Task\Bean\Annotation\Task;
- // use AlibabaCloud\Client\AlibabaCloud;
- // use AlibabaCloud\Client\Exception\ClientException;
- // use AlibabaCloud\Client\Exception\ServerException;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use AlibabaCloud\Ecs\Ecs;
- /**
- * 阿里信息发送类
- * @Task("ali")
- */
- class My_composer_aliyunsms
- {
- /**
- * 发送短信验证码
- */
- public function sendCode($config,$phone,$code)
- {
- $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();
- return $result->toArray();
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- }
demo
- include 'My_composer_aliyunsms.php';
- $sms = new My_composer_aliyunsms();
- $phone='xxxxxx';
- $code='99999';
- $config = [
- 'accessKeyId' => 'LTAIMje******hS',
- 'accessSecret' => 'fMuQTLUrKQN******Z2m07',
- 'SignName' => '****',
- 'TemplateCode' => 'SMS_****04',
- 'regionId' => 'cn-hangzhou'
- ];
- $re = $sms->sendCode($config,$phone,$code);
- var_dump($re);
Tags: PHP阿里云短信 sdk
- 上一篇:PHP Cli 模式设置进程名称的方法
- 下一篇:PHP反射学习入门示例
相关文章
- ·php QQ第三方登陆SDK程序代码(2014-08-27)
- ·基于微信公众平台开发SDK(demo)扩展例子(2014-09-22)
- ·php版微信JS-SDK地理位置取街景实例(2018-10-30)
- ·php版本微信js-sdk支付接口类例子(2018-10-30)
- ·php实现基于微信公众平台开发SDK(demo)扩展的方法(2021-05-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)