tinkphp5,thinkcmf5 使用阿里云短信

首先在阿里云注册好短信所需要的模板、签名,以及AccessKey。
直接用composer安装阿里云php的sdk,

composer require alibabacloud/client

接着在需要的地方引入

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

然后就可以执行业务逻辑了

  public function send_sms()
    {
        $param= $this->request->param();
       $sms_info=DB::name('sms_setting')->where('id',1)->find();
       $TemplateParam=array(
        'code'=>10,//这里根据模板的变量来设置
       );
       $TemplateParam=json_encode($TemplateParam);
       AlibabaCloud::accessKeyClient($sms_info['accessKeyId'], $sms_info['accessKeySecret'])
                               ->regionId('cn-hangzhou')
                               ->asDefaultClient();

        if (isset($param['phone'])) {
       try {
           $result = AlibabaCloud::rpc()
                                 ->product('Dysmsapi')
                                 // ->scheme('https') // https | http
                                 ->version('2017-05-25')
                                 ->action('SendSms')
                                 ->method('POST')
                                 ->host('dysmsapi.aliyuncs.com')
                                 ->options([
                                               'query' => [
                                                 'RegionId' => "cn-hangzhou",
                                                 'PhoneNumbers' => $param['phone'],
                                                 'SignName' => "这里是签名",
                                                'TemplateCode' => "模板id",
                                                'TemplateParam' => $TemplateParam,
                                               ],
                                           ])
                                 ->request();
           print_r($result->toArray());
       } catch (ClientException $e) {
           echo $e->getErrorMessage() . PHP_EOL;
       } catch (ServerException $e) {
           echo $e->getErrorMessage() . PHP_EOL;
       }
    }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值