Alisms.php 4.6 KB

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