ThinkPHP5-- access Tencent cloud messaging API

Access Tencent cloud messaging API is quite simple, the most important is the first time Tencent cloud text messages if there are 100 free SMS to test for the developers is enough, I will teach you the following access Tencent cloud messaging.

The first step: Preparations

Tencent cloud first SMS text messaging signature and SMS text template

Fill in the information, such as about 1-2 hours to apply successfully. Then download it SMS SDK (Composer If you install, you can use Composer to download)

After the SDK documentation provided by Tencent cloud download, extend the new folder in a folder named below sms, Tencent cloud SDK which all classes into the src folder under the sms
Step 2: Connect SMS API

  use Qcloud\sms\SmsSingleSender;

  //
腾讯短信验证码 public function tenxun(){ // 短信应用 SDK AppID $appid = 1400009099; // SDK AppID 以1400开头 // 短信应用 SDK AppKey $appkey = "10747e9376c79fc1b523bf7f33dc16"; // 需要发送短信的手机号码 $phoneNumbers = input("phone"); // 短信模板 ID,需要在短信控制台中申请 $templateId = 517396; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 $smsSign = "信息展示"; // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请 try { $ssender = new SmsSingleSender($appid, $appkey); $params = [rand(1000, 9999)];//生成随机数 $result = $ssender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", ""); $rsp = json_decode($result); return json(["result"=>$rsp->result,"code"=>$params]); } catch(\Exception $e) { echo var_dump($e); } }

遇到的问题及解决方法

问题一:$params(随机数)必须数组,这里有几个参数是根据短信模板的内容,例如:

问题二:刚引入use Qcloud\sms\SmsSingleSender,就把找不到SmsSingleSender类,原因是原来的src里面的所有类前面都加Qcloud,但我的目录是extend\sms,根本没有Qcloud,所以才报错。这样只要把sms前面加一个Qcloud目录,或者把sms里面的类的命名空间use前面都删了Qcloud就可以

Guess you like

Origin www.cnblogs.com/bushui/p/12174224.html