使用腾讯云手机短信作验证码

使用腾讯云短信官方代码和教程里推荐的超哥的插件,都可以正常收到短信,本文就是记录一下代码。

腾讯云发送短信,首先需要域名备案通过才行。然后创建短信签名sign_name,创建短信正文模板templateid,创建应用sdk_app_id。

使用腾讯云手机短信作验证码

使用腾讯云手机短信作验证码

sdk_app_id =    // 应用列表里创建的应用id
secret_id=      // 腾讯云的帐号信息-》访问管理-》访问密钥-》api密钥管理
secret_key=    // 上面的api密钥管理里面的
sign_name=     // 创建的短信签名,必须一模一样,而且需要通过审核才行
templateid=    // 创建的短信正文模板,必须通过审核,而且参数个数,值必须对应

使用腾讯云手机短信作验证码

官方php发送短信安装:github.com/TencentCloud/tencentclo...

  1. 建议中国大陆地区的用户设置腾讯云镜像源:composer config -g repos.packagist composer https://mirrors.tencent.com/composer/

  2. 执行命令 composer require tencentcloud/tencentcloud-sdk-php 添加依赖。

  3. 添加依赖

    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;
  4. 实现代码,注意上面的env里面的配置

         $yanzhengma = rand(1000, 9999);
         print_r($yanzhengma);
         $phone = [
             13333333333,
             15555555555,
         ];
    try {
             // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
             // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
             // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
             $cred = new Credential(env('secret_id'), env('secret_key'));
             // 实例化一个http选项,可选的,没有特殊需求可以跳过
             $httpProfile = new HttpProfile();
             $httpProfile->setEndpoint("sms.tencentcloudapi.com");
    
             // 实例化一个client选项,可选的,没有特殊需求可以跳过
             $clientProfile = new ClientProfile();
             $clientProfile->setHttpProfile($httpProfile);
             // 实例化要请求产品的client对象,clientProfile是可选的
             $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
    
             // 实例化一个请求对象,每个接口都会对应一个request对象
             $req = new SendSmsRequest();
    
             $params = array(
                 "PhoneNumberSet" => array(strval($phone[0])),
                 "SmsSdkAppId" => env('sdk_app_id'),
                 "SignName" => env('sign_name'),
                 "TemplateId" => env('templateid'),
                 "TemplateParamSet" => array(strval($yanzhengma))
             );
             $req->fromJsonString(json_encode($params));
    
             // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
             $resp = $client->SendSms($req);
    
             // 输出json格式的字符串回包
             print_r($resp->toJsonString());
         } catch (TencentCloudSDKException $e) {
             echo $e;
         }

使用超哥的插件:github.com/overtrue/easy-sms
要根据上面文档里对应的腾讯云配置,修改一下

使用腾讯云手机短信作验证码

app/easysms.php
<?php

return [
    // HTTP 请求的超时时间(秒)
    'timeout' => 5.0,

    // 默认发送配置
    'default' => [
        // 网关调用策略,默认:顺序调用
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

        // 默认可用的发送网关
        'gateways' => [
            'qcloud',
        ],
    ],
    // 可用的网关配置
    'gateways' => [
        'errorlog' => [
            'file' => '/tmp/easy-sms.log',
        ],
        'qcloud' => [
            'sdk_app_id' => env('sdk_app_id'),
            'secret_id' => env('secret_id'),
            'secret_key' => env('secret_key'),
            'sign_name' => env('sign_name'),
        ],
    ],
];

实现代码:

        $yanzhengma = rand(1000, 9999);
        print_r($yanzhengma);
        $sms = app('easysms');
        $phone = [
            13333333333,
            15555555555,
        ];
        try {
            $sms->send($phone[0], [
                'template' => env('templateid'),
                // 你在腾讯云配置的"短信正文”的模板ID
                'data' => [
                    // data数组的内容对应于腾讯云“短信正文“里的变量
                    $yanzhengma,
                    // 变量1
                ],
            ]);
        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
            $message = $exception->getException('qcloud')->getMessage();
            dd($message);
        }

dd($message);这个调试代码非常有用:

"signature format is incorrect or signature is not approved" // app/Http/Controllers/UsersController.php:52

这个出错一般是sign_name没有填写正确,必须是已经审核通过的签名,而且要一模一样,如果是中文,就填中文。

"The SecretId is not found, please ensure that your SecretId is correct." // app/Http/Controllers/UsersController.php:52

secret_id 没有填写正确

"The provided credentials could not be validated. Please check your signature is correct."

secret_key没有填写正确。

"template is not approved or not exist"

templateid没有填写正确。

"request content does not match the template content" 

templateid填写正确,但对应的参数不匹配,比如短信正文模板里有两个参数,实现代码里只给一个参数

                'data' => [
                    // data数组的内容对应于腾讯云“短信正文“里的变量
                    $yanzhengma,
                    // 变量1
                    4
                         // 变量2 4分钟后实效
                ],

腾讯云短信调试地址:console.cloud.tencent.com/api/expl...

"TemplateParamSet" => array( strval($yanzhengma), "4" )

使用腾讯云手机短信作验证码

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!