Aliyun.php 2.7 KB

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