欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 16:45:55
首先安装 composer require alibabacloud/sdk 接着看alibabacloud官网文档 示例 my_com...

首先安装

composer require alibabacloud/sdk

接着看alibabacloud官网文档

示例

my_composer_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_composer_aliyunsms
{
  /**
   * 发送短信验证码
   */
  public function sendcode($config,$phone,$code)
  {
    $param = [
      'code' => $code
    ];
    alibabacloud::accesskeyclient($config['accesskeyid'], $config['accesssecret'])
      ->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_composer_aliyunsms.php';
 $sms = new my_composer_aliyunsms();
 $phone='xxxxxx';
 $code='99999'; 
 $config = [
  'accesskeyid' => 'ltaimje******hs',
  'accesssecret' => 'fmuqtlurkqn******z2m07',
  'signname' => '****',
  'templatecode' => 'sms_****04',
  'regionid' => 'cn-hangzhou'
 ];
 $re = $sms->sendcode($config,$phone,$code);  
 var_dump($re);

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