123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server\sms\engine;
-
-
- use TencentCloud\Sms\V20190711\SmsClient;
- use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Credential;
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
-
-
-
- class TcSms extends Server
- {
- protected $config;
- protected $mobile;
- protected $template_id;
- protected $template_param ;
-
- public function __construct($config)
- {
- if (empty($config)) {
- $this->error = '请联系管理员配置参数';
- return false;
- }
- $this->config = $config;
- }
-
-
-
-
- public function setMobile($mobile = '')
- {
- $this->mobile = $mobile;
- return $this;
- }
-
-
-
-
- public function setTemplateId($template_id = null)
- {
- $this->template_id = $template_id;
- return $this;
- }
-
-
-
-
- public function setTemplateParam($template_param = null)
- {
- $this->template_param = $template_param;
- return $this;
- }
-
-
-
-
-
- public function send()
- {
- try {
- $cred = new Credential($this->config['app_key'], $this->config['secret_key']);
- $httpProfile = new HttpProfile();
- $httpProfile->setEndpoint("sms.tencentcloudapi.com");
-
- $clientProfile = new ClientProfile();
- $clientProfile->setHttpProfile($httpProfile);
-
- $client = new SmsClient($cred, "", $clientProfile);
- $req = new SendSmsRequest();
-
- $params = [
- 'PhoneNumberSet' => ['+86'.$this->mobile],
- 'TemplateID' => $this->template_id,
- 'Sign' => $this->config['sign'],
- 'TemplateParamSet' => $this->template_param,
- 'SmsSdkAppid' => $this->config['app_id'],
- ];
- $req->fromJsonString(json_encode($params));
-
- $resp = json_decode($client->SendSms($req)->toJsonString(), true);
-
- if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
- return $resp;
- } else {
- $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp);
- throw new \Exception('短信错误:' . $message);
- }
-
- } catch (TencentCloudSDKException $e) {
- $this->error = $e->getMessage();
- return false;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
- }
|