Aliyunsms.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\aliyunsms\library;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. class Aliyunsms
  5. {
  6. private $_params = [];
  7. protected $error = '';
  8. protected $config = [];
  9. protected static $instance = null;
  10. protected $statusStr = array(
  11. "0" => "短信发送成功",
  12. "-1" => "参数不全",
  13. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  14. "30" => "密码错误",
  15. "40" => "账号不存在",
  16. "41" => "余额不足",
  17. "42" => "帐户已过期",
  18. "43" => "IP地址限制",
  19. "50" => "内容含有敏感词"
  20. );
  21. /**
  22. * Aliyunsms constructor.
  23. * @param array $options
  24. * @throws \AlibabaCloud\Client\Exception\ClientException
  25. */
  26. public function __construct($options = [])
  27. {
  28. if ($config = get_addon_config('aliyunsms')) {
  29. $this->config = array_merge($this->config, $config);
  30. }
  31. $this->config = array_merge($this->config, is_array($options) ? $options : []);
  32. AlibabaCloud::accessKeyClient($this->config['accessKeyId'], $this->config['accessSecret'])
  33. ->regionId($this->config['regionId'])
  34. ->asDefaultClient();
  35. }
  36. /**
  37. * 单例
  38. * @param array $options 参数
  39. * @return Aliyunsms
  40. */
  41. public static function instance($options = [])
  42. {
  43. if (is_null(self::$instance)) {
  44. self::$instance = new static($options);
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 立即发送短信
  50. *
  51. * @return boolean
  52. * @throws \AlibabaCloud\Client\Exception\ClientException
  53. * @throws \AlibabaCloud\Client\Exception\ServerException
  54. */
  55. public function send()
  56. {
  57. $this->error = '';
  58. $params = $this->_params();
  59. $params_post = [
  60. 'code' => $params['msg']
  61. ];
  62. $result = AlibabaCloud::rpc()
  63. ->product('Dysmsapi')
  64. // ->scheme('https') // https | http
  65. ->version('2017-05-25')
  66. ->action('SendSms')
  67. ->method('POST')
  68. ->host('dysmsapi.aliyuncs.com')
  69. ->options([
  70. 'query' => [
  71. 'RegionId' => $this->config['regionId'],
  72. 'PhoneNumbers' => $this->_params['mobile'],
  73. 'SignName' => $this->config['signName'],
  74. 'TemplateCode' => $this->config['templateCode'],
  75. 'TemplateParam' => json_encode($params_post),
  76. ],
  77. ])
  78. ->request();
  79. $result = $result->toArray();
  80. if ($result['Code'] == "OK") {
  81. return true;
  82. } else {
  83. $this->error = $result['Message'];
  84. }
  85. return false;
  86. }
  87. private function _params()
  88. {
  89. return $this->_params;
  90. }
  91. /**
  92. * 获取错误信息
  93. * @return string
  94. */
  95. public function getError()
  96. {
  97. return $this->error;
  98. }
  99. /**
  100. * 接收手机
  101. * @param string $mobile 手机号码
  102. * @return Aliyunsms
  103. */
  104. public function mobile($mobile = '')
  105. {
  106. $this->_params['mobile'] = $mobile;
  107. return $this;
  108. }
  109. /**
  110. * 短信内容
  111. * @param string $msg 短信内容
  112. * @return Aliyunsms
  113. */
  114. public function msg($msg = '')
  115. {
  116. $this->_params['msg'] = $msg;
  117. return $this;
  118. }
  119. }