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
Application Templates
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.
The above steps are completed, get the AppID, AppKey, signature ID, template ID
Third, install dependencies
PHP is generally used composer Installation depends
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
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
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
which is received by the phone
so that you're done you
Thanks https://www.cnblogs.com/mg007/p/11053269.html