PHP接入阿里云短信(composer安装方式)

  • 环境准备

  • 安装composer并切换到阿里云源 composer config repo.packagist composer https://mirrors.aliyun.com/composer/ https://mirrors.aliyun.com/composer/ 

     注意:如果所有项目都是用阿里云源则执行  composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ https://mirrors.aliyun.com/composer/ 

          取消配置:composer config --unset repos.packagist

  • 进到项目根目录

  • 根据阿里云示例安装 composer require alibabacloud/smsintl

  • Demo调用文件如下:

     1 <?php
     2 namespace service;
     3 use think\Env;
     4 use AlibabaCloud\Client\AlibabaCloud;
     5 use AlibabaCloud\Client\Exception\ClientException;
     6 use AlibabaCloud\Client\Exception\ServerException;
     7 use think\Log;
     8 /**
     9  * 阿里云短信验证码发送类
    10  * @author Administrator
    11  *
    12  */
    13 
    14 class AliyunSms {
    15 
    16     // 保存错误信息
    17     public $error;
    18     // Access Key ID
    19     private $accessKeyId = '';
    20     // Access Access Key Secret
    21     private $accessKeySecret = '';
    22     // 签名
    23     private $signName = '';
    24     // 模版ID
    25     private $templateCode = '';
    26 
    27     public function __construct($cofig = array()) {
    28         if (empty($cofig)) {
    29             $cofig = array (
    30                     'accessKeyId' => Env::get('ALIYUN_SMS.ACCCESS_KEY_ID'),
    31                     'accessKeySecret' => Env::get('ALIYUN_SMS.ACCCESS_KEY_SECRET'),
    32                     'signName' => Env::get('ALIYUN_SMS.SIGN_NAME'),
    33                     'templateCode' => 'SMS_201455661'
    34             );
    35         }
    36         
    37         // 配置参数
    38         $this->accessKeyId = $cofig ['accessKeyId'];
    39         $this->accessKeySecret = $cofig ['accessKeySecret'];
    40         $this->signName = $cofig ['signName'];
    41         $this->templateCode = $cofig ['templateCode'];
    42     }
    43     
    44     
    45     /**
    46      * 单条短信下发
    47      * @param unknown $mobile
    48      * @param unknown $code
    49      * @return boolean
    50      */
    51     public function smsaliyun ($mobile, $code) {
    52         AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
    53             ->regionId('cn-hangzhou')
    54             ->asDefaultClient();
    55 
    56         try {
    57             $result = AlibabaCloud::rpc()
    58                         ->product('Dysmsapi')
    59                      // ->scheme('https') // https | http
    60                         ->version('2017-05-25')
    61                         ->action('SendSms')
    62                         ->method('POST')
    63                         ->host('dysmsapi.aliyuncs.com')
    64                         ->options([
    65                                 'query' => [
    66                                         'RegionId' => "cn-hangzhou",
    67                                         'PhoneNumbers' => $mobile,
    68                                         'SignName' => $this->signName,
    69                                         'TemplateCode' => $this->templateCode,
    70                                         'TemplateParam' => '{"code":"' . $code . '"}'
    71                                 ],
    72                         ])
    73                         ->request();
    74             return $result->toArray();
    75         } catch (ClientException $e) {
    76             Log::ERROR('阿里云短信发送失败',$e->getErrorMessage());
    77             return false;
    78         } catch (ServerException $e) {
    79             Log::ERROR('阿里云短信发送失败',$e->getErrorMessage());
    80             return false;
    81         }
    82     }
    83 }
posted @ 2021-01-06 18:10  老兵不扛枪  阅读(1131)  评论(0编辑  收藏  举报