TP3.2阿里云短信验证代码

阿里云短信发送本身无难度,只是用来记录而已。
从阿里云中下载短信DEMO,配置好AccessKeyId和AccessKeySecret即可。

<?php
namespace Home\Controller;
use Think\Controller;
use Aliyun\Core\Config as Config;
use Aliyun\Core\Profile\DefaultProfile as DefaultProfile;
use Aliyun\Core\DefaultAcsClient as DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest as SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest as QuerySendDetailsRequest;

// +----------------------------------------------------------------------
// | @explain  阿里云短信验证接口
// +----------------------------------------------------------------------
class AliYunController extends Controller {
    static $acsClient = null;

    public function  __construct()
    {
        parent::__construct();
        ini_set("display_errors", "on");
        require_once '/phpstudy/www/*****/ThinkPHP/Library/Aliyun/api_sdk/vendor/autoload.php';
        // 加载区域结点配置
        Config::load();
    }

    /**
     * 发送短信
     * @return stdClass
     */
    public function sendSms() {
        /*记录日志*/
        Add_LOG('AliYun',__FUNCTION__.' '.__LINE__.' phone,code:'.json_encode($_GET));
        $phone = I('phone');
        $code  = I('code');
        if (empty($phone) || empty($code)) {
            echo json_encode(array('status'=>202,'message'=>'参数异常'));exit;
        }
        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        // 必填,设置短信接收号码
        $request->setPhoneNumbers($phone);
        // 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $request->setSignName("****");
        // 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $request->setTemplateCode("SMS_*********");
        // 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
        $request->setTemplateParam(json_encode(Array(  // 短信模板中字段的值
            "code"=>$code,
            "product"=>"dsd"
        ), JSON_UNESCAPED_UNICODE));
        Add_LOG('AliYun',__FUNCTION__.' '.__LINE__.' request:'.json_encode($request));
        // 发起访问请求
        $acsResponse = static::getAcsClient()->getAcsResponse($request);
        Add_LOG('AliYun',__FUNCTION__.' '.__LINE__.' acsResponse:'.json_encode($acsResponse));
        // var_dump($acsResponse);
        return $acsResponse;
    }

    /**
     * 取得AcsClient
     *
     * @return DefaultAcsClient
     */
    public static function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dysmsapi";

        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";

        $accessKeyId = "**********"; // AccessKeyId

        $accessKeySecret = "****************"; // AccessKeySecret

        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if(static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }
}
//有两个注意点:1、use Aliyun\Core\Config 后面需要加as Config,否则会报错找不到类Config;2、需要继承父类parent::__construct()。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值