Aliyun.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library\sms\engine;
  10. use app\common\library\sms\package\aliyun\SignatureHelper;
  11. /**
  12. * 阿里云短信模块引擎
  13. * Class Aliyun
  14. * @package app\common\library\sms\engine
  15. */
  16. class Aliyun extends Server
  17. {
  18. private $config;
  19. /**
  20. * 构造方法
  21. * Qiniu constructor.
  22. * @param $config
  23. */
  24. public function __construct($config)
  25. {
  26. $this->config = $config;
  27. }
  28. /**
  29. * 发送短信通知
  30. * @param $msgType
  31. * @param $templateParams
  32. * @return bool|\stdClass
  33. */
  34. public function sendSms($msgType, $templateParams)
  35. {
  36. $params = [];
  37. // *** 需用户填写部分 ***
  38. // 必填: 短信接收号码
  39. $params["PhoneNumbers"] = $this->config[$msgType]['accept_phone'];
  40. // 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  41. $params["SignName"] = $this->config['sign'];
  42. // 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  43. $params["TemplateCode"] = $this->config[$msgType]['template_code'];
  44. // 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  45. $params['TemplateParam'] = $templateParams;
  46. // 可选: 设置发送短信流水号
  47. // $params['OutId'] = "12345";
  48. // 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  49. // $params['SmsUpExtendCode'] = "1234567";
  50. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  51. if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  52. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  53. }
  54. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  55. $helper = new SignatureHelper;
  56. // 此处可能会抛出异常,注意catch
  57. $response = $helper->request(
  58. $this->config['AccessKeyId']
  59. , $this->config['AccessKeySecret']
  60. , "dysmsapi.aliyuncs.com"
  61. , array_merge($params, [
  62. "RegionId" => "cn-hangzhou",
  63. "Action" => "SendSms",
  64. "Version" => "2017-05-25",
  65. ])
  66. // 选填: 启用https
  67. , true
  68. );
  69. // 记录日志
  70. log_write([
  71. 'config' => $this->config,
  72. 'params' => $params
  73. ]);
  74. log_write($response);
  75. $this->error = $response->Message;
  76. return $response->Code === 'OK';
  77. }
  78. }