composer安装阿里云短信

  • 环境准备

  • 安装composer并切换到阿里云源

  • cd到项目根目录

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

  • 直接在api/controller/common源码里引用

  •   use AlibabaCloudClientAlibabaCloud;
      use AlibabaCloudClientExceptionClientException;
      use AlibabaCloudClientExceptionServerException;
      public function smsaliyun(){
              $req = $this->request->param();
              $vali = $this->validate($req, [
                  'mobile' => 'require|integer',
                  'code' => 'require|integer',
              ]);
              if (true !== $vali) {
                  $this->error('参数错误', $vali);
              }
              AlibabaCloud::accessKeyClient('akey', 'skey')
                  ->regionId('cn-hangzhou')
                  ->asDefaultClient();
              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' => $req['mobile'],
                              'SignName' => "掌上医堂",
                              'TemplateCode' => "SMS_199920034",
      //                        'TemplateParam' => "{"code":"123456"}",
                              'TemplateParam' => "{"code":"".$req['code'].""}",
                          ],
                      ])
                      ->request();
                   $this->success('发送成功', $result->toArray());
              } catch (ClientException $e) {
                  $this->error('发送失败',$e->getErrorMessage() . PHP_EOL);
              } catch (ServerException $e) {
                  $this->error('发送失败',$e->getErrorMessage() . PHP_EOL);
              }
          }
    
原文地址:https://www.cnblogs.com/bufeetu/p/13780642.html