TencentSms.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils\Sms;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-31 15:18
  12. // +----------------------------------------------------------------------
  13. use app\model\api\SmsSend;
  14. use app\model\api\SmsTpl;
  15. use TencentCloud\Common\Credential;
  16. use TencentCloud\Common\Profile\ClientProfile;
  17. use TencentCloud\Common\Profile\HttpProfile;
  18. use TencentCloud\Ic\V20190307\Models\SendSmsResponse;
  19. use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
  20. use TencentCloud\Sms\V20190711\SmsClient;
  21. class TencentSms {
  22. /**
  23. * 转义的模板信息[短信]
  24. */
  25. function SmsTplParm($content) {
  26. $data = func_get_args();
  27. foreach ($data[1] as $k => $v) {
  28. $content = str_replace('{' . $k . '}', $v, $content);
  29. }
  30. return $content;
  31. }
  32. /**
  33. * 发送模板的数据
  34. * @param type $tpl
  35. * @param type $code
  36. */
  37. function SmsSendTmplete($tel, $code, $array = []) {
  38. $sData = SmsTpl::where("code",$code)->find();
  39. $r = false;
  40. $error = '';
  41. $req = null;
  42. if (!empty($sData)) {
  43. $req = $this->SmsSend($tel, $code, $array);
  44. if (!empty($req['Code']) && $req['Code'] == 'OK') {
  45. $r = true;
  46. } else {
  47. $error = $req['Message'];
  48. }
  49. $datax['mobile'] = $tel;
  50. $datax['content'] = $this->SmsTplParm($sData['content'], $array);
  51. $datax['time'] = time();
  52. $datax['code'] = $code;
  53. if ($r) {
  54. $datax['is_success'] = 1;
  55. } else {
  56. $datax['is_success'] = -1;
  57. $datax['error'] = $error;
  58. }
  59. ( new SmsSend)->insert($datax);
  60. }
  61. return $req;
  62. }
  63. /**
  64. * 发送短信
  65. * @param $tel
  66. * @param $code
  67. * @param $array
  68. */
  69. public function SmsSend($tel,$code,$array) {
  70. try {
  71. $config = config('SMS');
  72. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  73. $httpProfile = new HttpProfile();
  74. $httpProfile->setEndpoint("sms.tencentcloudapi.com");
  75. $clientProfile = new ClientProfile();
  76. $clientProfile->setHttpProfile($httpProfile);
  77. $client = new SmsClient($cred, "", $clientProfile);
  78. $req = new SendSmsRequest();
  79. $params = [
  80. 'TemplateID' => $code,
  81. 'SmsSdkAppid' => $config['SmsAppId'],
  82. 'Sign' => $config['Sign'],
  83. 'PhoneNumberSet' => ['+86' . $tel],
  84. ];
  85. ksort($array);
  86. $params["TemplateParamSet"] = [];
  87. foreach ($array as $v) {
  88. $params["TemplateParamSet"][] = $v;
  89. }
  90. $req->fromJsonString(json_encode($params));
  91. $resp = $client->SendSms($req);
  92. $data = json_decode($resp->toJsonString(),true);
  93. if($data['SendStatusSet'][0]['Code'] == 'Ok') {
  94. return ['Code'=>'OK','Message'=>'ok'];
  95. } else {
  96. return ['Code'=>'NO','Message'=>$data['SendStatusSet'][0]['Code']];
  97. }
  98. }catch(TencentCloudSDKException $e) {
  99. return ['Code'=>'no','Message'=>$e->getMessage()];
  100. }
  101. }
  102. private function fetchContent($url) {
  103. $ch = curl_init();
  104. curl_setopt($ch, CURLOPT_URL, $url);
  105. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  106. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  107. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  108. "x-sdk-client" => "php/2.0.0"
  109. ));
  110. if(substr($url, 0,5) == 'https') {
  111. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  113. }
  114. $rtn = curl_exec($ch);
  115. if($rtn === false) {
  116. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  117. }
  118. curl_close($ch);
  119. return $rtn;
  120. }
  121. }