Tencent cloud SMS development [PHP]

Tencent cloud SMS development [PHP]

Tencent Cloud SMS Console: https: //console.cloud.tencent.com/sms

Tencent cloud SMS PHP SDK: https: //github.com/qcloudsms/qcloudsms_php

Use Thinkphp 5.1 framework

A signature

Search Tencent cloud console in SMS or open the link above, enter into the SMS console, signed application and templates

Signing request

Here Insert Picture Description

Application Templates

Here Insert Picture Description

Second, the acquisition SDK AppID and AppKey

After passing through after the audit, to obtain a series of data we need to develop, we can develop a test

Cloud SMS app SDK AppID and AppKey available at the console's messaging application information, such as your application has not been added, please add the SMS console application.
Here Insert Picture Description
96.png)]

The above steps are completed, get the AppID, AppKey, signature ID, template ID

Third, install dependencies

PHP is generally used composer Installation depends
Here Insert Picture Description
if the composer is not installed, please install the composer

Execute the following command in the project root of our TP5.1
composer require qcloudsms/qcloudsms_php
Here Insert Picture Description
installed dependence later, it is written Interface

Fourth, send text messages

Configuring SMS AppID, AppKey, signature ID, template ID

Duanxin.php created in the configuration file
Here Insert Picture Description

Sending the encapsulated SMS code

namespace app\index\controller;
use Qcloud\Sms\SmsSingleSender;

class Sms
{
    /**
     * 腾讯云 发送短信验证码
     * @param $phone:手机号码
     * @param $code:验证码
     */
    public static function SendSmsCode($phone, $code)
    {
        $app_id = config('duanxin.app_id');
        $app_key = config('duanxin.app_key');
        $template_id = config('duanxin.template_id');
        $sms_sign = config('duanxin.sms_sign');

        try {
            $sender = new SmsSingleSender($app_id, $app_key);
            $params = [$code];
            $result = $sender->sendWithParam("86", $phone, $template_id,
                $params, $sms_sign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
            $rsp = json_decode($result);
            if($rsp->result == 0){//0代表成功
                return '发送成功';
            }
            \Log::error('发送短信验证码失败:'.$result);
        } catch(\Exception $e) {
        }
        return '发送失败';
    }
}

The main method call

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $params = $this->request->param();
        $phone = $params['phone'];
        $code = mt_rand(100000,999999);   //生产随机6位验证码
        //这里可以将验证码存入到缓存当中去,以手机号作为标识
        return Sms::SendSmsCode($phone,$code);
    }

}

Verify sent successfully

Using the API tools to test the interface, note that this is POST Interface
Here Insert Picture Description
which is received by the phone
Here Insert Picture Description
so that you're done you

Thanks https://www.cnblogs.com/mg007/p/11053269.html

Published 50 original articles · won praise 64 · views 4322

Guess you like

Origin blog.csdn.net/qq_45163122/article/details/104107182