分享一个腾讯云短信发送的方法 超级简单
这个是在腾讯云官网做一个demo 根据语言生成代码可以直接发送
API Explorer - 云 API - 控制台 (tencent.com)
可以使用composer安装一下腾讯的短信
composer require tencentcloud/tencentcloud-sdk-php
下面就是一个PHP的demo 正常发送短信后根据自己的业务进行验证码校验
<?php namespace app\api\controller; //require_once 'vendor/autoload.php'; use TencentCloud\Common\Credential; use TencentCloud\Common\Profile\ClientProfile; use TencentCloud\Common\Profile\HttpProfile; use TencentCloud\Common\Exception\TencentCloudSDKException; use TencentCloud\Sms\V20210111\SmsClient; use TencentCloud\Sms\V20210111\Models\SendSmsRequest; use app\common\controller\Wap; class Sms extends Wap { public function putsms(){ $rand = mt_rand(000000,999999); $result = request()->post(); $phone = array($result['phone']); if (Cache::get($phone)!=''){ return json(['code'=>-1,'msg'=>'抱歉,你5分钟之内已请求一次短信','data'=>'']); }try { // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 $cred = new Credential("xxxxxxxxxxxxx", "xxxxxxxxxxxxxxxx"); // 实例化一个http选项,可选的,没有特殊需求可以跳过 $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 实例化一个client选项,可选的,没有特殊需求可以跳过 $clientProfile = new ClientProfile(); $clientProfile->setHttpProfile($httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 $client = new SmsClient($cred, "ap-beijing", $clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 $req = new SendSmsRequest(); $params = array( "PhoneNumberSet" => $phone, //电话号 "SmsSdkAppId" => "xxxxxxx", //1400开头的sdkappid "SignName" => "腾讯云小程序", //模板的名称 查看自己申请的 "TemplateId" => "xxxxxxx", 模板id "TemplateParamSet" => array("$rand") //根据自己业务 可以假如时间验证码等信息发给用户 //例如:验证码为:{1},若非本人操作,请勿泄露,{2}分钟有效。 //"TemplateParamSet" => array("123456","10") ); $req->fromJsonString(json_encode($params)); // 返回的resp是一个SendSmsResponse的实例,与请求对象对应 $resp = $client->SendSms($req); // 输出json格式的字符串回包 $resp->toJsonString(); Cache::set($result['mobile'],$rand,300); //写入缓存 300秒有效 //如果读取手机号的话 Cache::get('手机号') return json(['code'=>0,'msg'=>'短信发送成功','data'=>'']); } catch(TencentCloudSDKException $e) { echo $e; } } }