Alisms.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace addons\alisms\library;
  3. use think\Config;
  4. /**
  5. * 阿里大于SMS短信发送
  6. */
  7. class Alisms
  8. {
  9. private $_params = [];
  10. public $error = '';
  11. protected $config = [];
  12. public function __construct($options = [])
  13. {
  14. if ($config = get_addon_config('alisms'))
  15. {
  16. $this->config = array_merge($this->config, $config);
  17. }
  18. $this->config = array_merge($this->config, is_array($options) ? $options : []);
  19. }
  20. /**
  21. * 单例
  22. * @param array $options 参数
  23. * @return Alisms
  24. */
  25. public static function instance($options = [])
  26. {
  27. if (is_null(self::$instance))
  28. {
  29. self::$instance = new static($options);
  30. }
  31. return self::$instance;
  32. }
  33. /**
  34. * 设置签名
  35. * @param string $sign
  36. * @return Alisms
  37. */
  38. public function sign($sign = '')
  39. {
  40. $this->_params['SignName'] = $sign;
  41. return $this;
  42. }
  43. /**
  44. * 设置参数
  45. * @param array $param
  46. * @return Alisms
  47. */
  48. public function param(array $param = [])
  49. {
  50. foreach ($param as $k => &$v)
  51. {
  52. $v = (string) $v;
  53. }
  54. unset($v);
  55. $this->_params['TemplateParam'] = json_encode($param);
  56. return $this;
  57. }
  58. /**
  59. * 设置模板
  60. * @param string $code 短信模板
  61. * @return Alisms
  62. */
  63. public function template($code = '')
  64. {
  65. $this->_params['TemplateCode'] = $code;
  66. return $this;
  67. }
  68. /**
  69. * 接收手机
  70. * @param string $mobile 手机号码
  71. * @return Alisms
  72. */
  73. public function mobile($mobile = '')
  74. {
  75. $this->_params['PhoneNumbers'] = $mobile;
  76. return $this;
  77. }
  78. /**
  79. * 立即发送
  80. * @return boolean
  81. */
  82. public function send()
  83. {
  84. $this->error = '';
  85. $params = $this->_params();
  86. $params['Signature'] = $this->_signed($params);
  87. $response = $this->_curl($params);
  88. if ($response !== FALSE)
  89. {
  90. $res = (array) json_decode($response, TRUE);
  91. if (isset($res['Code']) && $res['Code'] == 'OK')
  92. return TRUE;
  93. $this->error = isset($res['Message']) ? $res['Message'] : 'InvalidResult';
  94. }
  95. else
  96. {
  97. $this->error = 'InvalidResult';
  98. }
  99. return FALSE;
  100. }
  101. /**
  102. * 获取错误信息
  103. * @return array
  104. */
  105. public function getError()
  106. {
  107. return $this->error;
  108. }
  109. private function _params()
  110. {
  111. return array_merge([
  112. 'AccessKeyId' => $this->config['key'],
  113. 'SignName' => isset($this->config['sign']) ? $this->config['sign'] : '',
  114. 'Action' => 'SendSms',
  115. 'Format' => 'JSON',
  116. 'Version' => '2017-05-25',
  117. 'SignatureVersion' => '1.0',
  118. 'SignatureMethod' => 'HMAC-SHA1',
  119. 'SignatureNonce' => uniqid(),
  120. 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
  121. ], $this->_params);
  122. }
  123. private function percentEncode($string)
  124. {
  125. $string = urlencode($string);
  126. $string = preg_replace('/\+/', '%20', $string);
  127. $string = preg_replace('/\*/', '%2A', $string);
  128. $string = preg_replace('/%7E/', '~', $string);
  129. return $string;
  130. }
  131. private function _signed($params)
  132. {
  133. $sign = $this->config['secret'];
  134. ksort($params);
  135. $canonicalizedQueryString = '';
  136. foreach ($params as $key => $value)
  137. {
  138. $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
  139. }
  140. $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  141. $signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true));
  142. return $signature;
  143. }
  144. private function _curl($params)
  145. {
  146. $uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  150. curl_setopt($ch, CURLOPT_URL, $uri);
  151. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  152. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  153. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  156. $reponse = curl_exec($ch);
  157. curl_close($ch);
  158. return $reponse;
  159. }
  160. }