config = $this->getConfig(); //如果使用语音短信 更换成语音短信模板 if ($this->config['isVoice'] == 1) { $this->config['template'] = $this->config['voiceTemplate']; //语音短信 需要另行设置Aappid 与Appkey $this->appid = $this->config['voiceAppid']; $this->appkey = $this->config['voiceAppkey']; } else { $this->appid = $this->config['appid']; $this->appkey = $this->config['appkey']; } } /** * 短信发送行为 * @param Sms $params * @return boolean */ public function smsSend(&$params) { //腾讯 try { /* 必要步骤: * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。 * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。 * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人, * 以免泄露密钥对危及你的财产安全。 * CAM密匙查询: https://console.cloud.tencent.com/cam/capi*/ $cred = new Credential("IKIDhLn151pT0bItU3UsBQabWNYacdVIuoYF", "lPzb9sTGuNsZ6c3MugUOAmNq9874vrlb"); //$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-singapore,地域列表可参考https://intl.cloud.tencent.com/document/api/382/40466?lang=en#region-list $client = new SmsClient($cred, "ap-singapore", $clientProfile); // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。 $req = new SendSmsRequest(); /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */ $req->SmsSdkAppId = "2400000981"; /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */ $req->SignName = ""; /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */ $req->ExtendCode = ""; /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/ $phone_number = $params['m_prefix'] . $params['mobile']; $req->PhoneNumberSet = array($phone_number); /* 国际/港澳台短信 SenderId: 中国大陆地区短信填空,默认未开通,如需开通请联系 [sms helper] */ $req->SenderId = ""; /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */ $req->SessionContext = "【turkey global】Your OTP code is {1} Don't share this code with others dated!"; /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */ $req->TemplateId = "2905310"; //2905133 /* 模板参数: 若无模板参数,则设置为空*/ $req->TemplateParamSet = array($params['code']); // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的 // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应 $resp = $client->SendSms($req); // 输出json格式的字符串回包 // print_r($resp->toJsonString()); // 也可以取出单个值。 // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义 // print_r($resp->TotalCount); return true; } catch(TencentCloudSDKException $e) { // echo $e; return true; } // AlibabaCloud::accessKeyClient('LTAI5tGjBhYq4u2e13f4cAQ6', 'X20XXcrKZHbGjpHHeTo3rfNuhMgeKg') // ->regionId('ap-southeast-1') // ->asGlobalClient(); // $phone_number = $params['m_prefix'] . $params['mobile']; // $result = AlibabaCloud::rpcRequest() // ->product('Dysmsapi') // ->host('dysmsapi.ap-southeast-1.aliyuncs.com') // ->version('2018-05-01') // ->action('SendMessageToGlobe') // ->method('POST') // ->options([ // 'query' => [ // "To" => $phone_number, // // "From" => "1234567890", // "Message" => "【Poly】Your OTP code is " . $params['code'] . " Don't share this code with others dated", // ], // ]) // ->request(); //// print_r($result->toArray()); // // return true; // if ($send_res['status']['code'] == 200) { // return true; // } else { // return true; // } // die; } /** * 短信发送通知 * @param array $params * @return boolean */ public function smsNotice(&$params) { $this->ConfigInit(); try { if ($this->config['isTemplateSender'] == 1) { $templateID = $this->config['template'][$params['template']]; if ($this->config['isVoice'] != 1) { //普通短信发送 $this->sender = new SmsSingleSender($this->appid, $this->appkey); $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params['msg']}"], $this->config['sign'], "", ""); } else { //语音短信发送 $this->sender = new TtsVoiceSender($this->appid, $this->appkey); //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段) $result = $this->sender->send("86", $params['mobile'], $templateID, [$params['msg']]); } } else { //判断是否是语音短信 if ($this->config['isVoice'] != 1) { $this->sender = new SmsSingleSender($this->appid, $this->appkey); //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数 $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", ""); } else { $this->sender = new SmsVoicePromptSender($this->appid, $this->appkey); //参数:国家码、手机号、语音类型(目前固定为2)、短信内容、播放次数(默认2次)、服务的原样返回的参数 $result = $this->sender->send('86', $params['mobile'], 2, $params['msg']); } } $rsp = (array)json_decode($result, true); if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') { return true; } else { //记录错误信息 $this->setError($rsp); return false; } } catch (\Exception $e) { var_dump($e); exit(); } } /** * 记录失败信息 * @param [type] $err [description] */ private function setError($err) { $this->sendError = $err; } /** * 获取失败信息 * @return [type] [description] */ public function getError() { return $this->sendError; } /** * 检测验证是否正确 * @param Sms $params * @return boolean */ public function smsCheck(&$params) { return true; } /** * 插件安装方法 * @return bool */ public function install() { return true; } /** * 插件卸载方法 * @return bool */ public function uninstall() { return true; } /** * 插件启用方法 * @return bool */ public function enable() { return true; } /** * 插件禁用方法 * @return bool */ public function disable() { return true; } }