PHP如何实现阿里云短信sdk灵活应用在项目中的方

网络编程 2021-07-04 23:05www.168986.cn编程入门
这篇文章主要介绍了PHP如何实现阿里云短信sdk灵活应用在项目中的方法,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧

安装

poser require alibabacloud/sdk

接着看alibabacloud官网文档

示例

My_poser_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_poser_aliyunsms
{
  /
    发送短信验证码
   /
  public function sendCode($config,$phone,$code)
  {
    $param = [
      'code' => $code
    ];
    AlibabaCloud::aessKeyClient($config['aessKeyId'], $config['aessSecret'])
      ->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_poser_aliyunsms.php';
 $sms = new My_poser_aliyunsms();
 $phone='xxxxxx';
 $code='99999'; 
 $config = [
  'aessKeyId' => 'LTAIMjehS',
  'aessSecret' => 'fMuQTLUrKQNZ2m07',
  'SignName' => '',
  'TemplateCode' => 'SMS_04',
  'regionId' => '-hangzhou'
 ];
 $re = $sms->sendCode($config,$phone,$code);  
 var_dump($re);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by