美文网首页
thinkphp5旧工程使用新版本腾讯云短信sdk

thinkphp5旧工程使用新版本腾讯云短信sdk

作者: ArslanRobot | 来源:发表于2023-11-05 12:38 被阅读0次

    增加腾讯云短信验证码,发现使用老项目安装腾讯云短信SDK报错composer版本不匹配

    zhb@bindeMacBook-Pro carmgr2022 % composer require tencentcloud/sms     Warning: This development build of Composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
    The "topthink/think-installer" plugin was skipped because it requires a Plugin API version ("^1.0") that does not match your Composer installation ("2.3.0"). You may need to run composer update with the "--no-plugins" option.
    ./composer.json has been updated
    The "topthink/think-installer" plugin was skipped because it requires a Plugin API version ("^1.0") that does not match your Composer installation ("2.3.0"). You may need to run composer update with the "--no-plugins" option.
    Running composer update tencentcloud/sms
    Loading composer repositories with package information
    Updating dependencies
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - topthink/think-installer v1.0.12 requires composer-plugin-api ^1.0 -> found composer-plugin-api[2.3.0] but it does not match the constraint.
        - topthink/framework v5.1.0 requires topthink/think-installer ~1.0 -> satisfiable by topthink/think-installer[v1.0.12].
        - topthink/framework is locked to version v5.1.0 and an update of this package was not requested.
    
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require tencentcloud/sms:*" to figure out if any version is installable, or "composer require tencentcloud/sms:^2.1" if you know which you need.
    

    老版本的thinkphp5不支持composer 2,需要降级回composer 1,

    composer self-update --1
    

    重新安装sdk

    composer require tencentcloud/sms
    

    若要恢复composer 2

    composer self-update --2
    

    短信发送

    <?php
    namespace app\web\service;
    
    require_once '/Users/xxx/dev/phppro/2022/carmgr2022/vendor/autoload.php';
    // 导入对应产品模块的client
    use TencentCloud\Sms\V20210111\SmsClient;
    // 导入要请求接口对应的Request类
    use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
    use TencentCloud\Common\Exception\TencentCloudSDKException;
    use TencentCloud\Common\Credential;
    // 导入可选配置类
    use TencentCloud\Common\Profile\ClientProfile;
    use TencentCloud\Common\Profile\HttpProfile;
    
    class UserService
    {
        public static function sendTxSms() {
            try {
                /* 必要步骤:
                * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
                * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
                * 您也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
                * 以免泄露密钥对危及您的财产安全。
                * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
                $cred = new Credential("SecretId", "SecretKey");
                //$cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));
    
    
                // 实例化一个http选项,可选的,没有特殊需求可以跳过
                $httpProfile = new HttpProfile();
                // 配置代理(无需要直接忽略)
                // $httpProfile->setProxy("https://ip:port");
                $httpProfile->setReqMethod("GET");  // post请求(默认为post请求)
                $httpProfile->setReqTimeout(30);    // 请求超时时间,单位为秒(默认60秒)
                $httpProfile->setEndpoint("sms.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)
    
    
                // 实例化一个client选项,可选的,没有特殊需求可以跳过
                $clientProfile = new ClientProfile();
                $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法(默认为HmacSHA256)
                $clientProfile->setHttpProfile($httpProfile);
    
    
                // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
                // 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
                $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
    
    
                // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
                $req = new SendSmsRequest();
    
    
                /* 填充请求参数,这里request对象的成员变量即对应接口的入参
                * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
                * 基本类型的设置:
                * 帮助链接:
                * 短信控制台: https://console.cloud.tencent.com/smsv2
                * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
    
    
                /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
                // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
                $req->SmsSdkAppId = "1400006666";
                /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
                // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
                $req->SignName = "xxxx";
                /* 模板 ID: 必须填写已审核通过的模板 ID */
                // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
                $req->TemplateId = "5273831";
                /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
                $req->TemplateParamSet = array("1234");
                /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
                * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
                $req->PhoneNumberSet = array("+8618561686156");
                /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
                $req->SessionContext = "";
                /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
                $req->ExtendCode = "";
                /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。*/
                $req->SenderId = "";
    
    
                // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
                // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
                $resp = $client->SendSms($req);
    
    
                // 输出json格式的字符串回包
                print_r($resp->toJsonString());
    
    
                // 也可以取出单个值。
                // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
                // print_r($resp->TotalCount);
    
    
                /* 当出现以下错误码时,快速解决方案参考
                * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
                * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
                * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
                * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
                * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
                */
            }
            catch(TencentCloudSDKException $e) {
                echo $e;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:thinkphp5旧工程使用新版本腾讯云短信sdk

          本文链接:https://www.haomeiwen.com/subject/gfbzidtx.html