zoukankan      html  css  js  c++  java
  • 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 thinkEnv;
       4 use AlibabaCloudClientAlibabaCloud;
       5 use AlibabaCloudClientExceptionClientException;
       6 use AlibabaCloudClientExceptionServerException;
       7 use thinkLog;
       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 }
  • 相关阅读:
    sabaki and leelazero
    apply current folder view to all folders
    string operation in powershell
    wirte function in powershell
    add environment path to powershell
    Module in powershell
    sql prompt
    vmware中鼠标在部分区域不能使用
    调整多个控件的dock的顺序
    行为型模型 策略模式
  • 原文地址:https://www.cnblogs.com/yanpeng1991/p/14242592.html
Copyright © 2011-2022 走看看