Sms.Class.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 腾讯云-短信
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/10/26
  7. * Time: 6:07 PM
  8. */
  9. namespace Util\TencentCloud;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Util\TencentCloud\CommonRequest;
  13. class Sms
  14. {
  15. /**
  16. * 腾讯云安全凭证
  17. * @var string $secret_id
  18. */
  19. private $secret_id;
  20. /**
  21. * 腾讯云安全凭证
  22. * @var string $secret_key
  23. */
  24. private $secret_key;
  25. private $apiUrl = 'https://sms.tencentcloudapi.com';
  26. private $SmsSdkAppid;
  27. private $Sign;
  28. public function __construct($secret_id='', $secret_key='', $SmsSdkAppid = '1400250992', $Sign = '源点计划')
  29. {
  30. $this->SmsSdkAppid = $SmsSdkAppid;
  31. $this->Sign = $Sign;
  32. $this->secret_id = $secret_id;
  33. $this->secret_key = $secret_key;
  34. }
  35. /**
  36. * 发送短信
  37. * 官网文档地址:https://cloud.tencent.com/document/api/382/38778
  38. * @param $templateId string 发送短信模板id
  39. * @param $mobile array 接受短信手机号
  40. * @param $templateParams array 短信内容参数
  41. */
  42. public function SendSms($templateId, $mobile, $templateParams)
  43. {
  44. $postData = [
  45. 'PhoneNumberSet' => $mobile,
  46. 'TemplateID' => $templateId,
  47. 'Sign' => $this->Sign,
  48. 'TemplateParamSet' => $templateParams,
  49. 'SmsSdkAppid' => $this->SmsSdkAppid, // 短信应用的唯一标识
  50. ];
  51. $header = CommonRequest::createCommonRequestHeader('ap-chengdu', $this->secret_id, $this->secret_key, 'sms', $postData, 'sms.tencentcloudapi.com', 'sms');
  52. $curl_option = [
  53. CURLOPT_HTTPHEADER => [
  54. 'Content-Type:application/json',
  55. 'X-TC-Action:SendSms',
  56. 'X-TC-Timestamp:'.$header['X-TC-Timestamp'],
  57. 'X-TC-Version:2019-07-11',
  58. 'X-TC-Region:'.$header['X-TC-Region'],
  59. 'Authorization:'.$header['Authorization'],
  60. ],
  61. ];
  62. //V(json_encode($postData, JSON_UNESCAPED_UNICODE));
  63. $reponse = request($this->apiUrl, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $curl_option);
  64. if($reponse['httpcode'] != 200){
  65. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  66. }else{
  67. $reponseData = json_decode($reponse['content'], true);
  68. if( isset($reponseData['Response']['Error']) ){
  69. return ResultWrapper::fail($reponseData['Response']['Error'], ErrorCode::$apiNotResult);
  70. }
  71. return ResultWrapper::success($reponseData['Response']['SendStatusSet']);
  72. }
  73. }
  74. }