Alisms.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\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 = 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. $param = array_filter($param);
  53. $this->_params['TemplateParam'] = $param ? json_encode($param) : '{}';
  54. return $this;
  55. }
  56. /**
  57. * 设置模板
  58. * @param string $code 短信模板
  59. * @return Alisms
  60. */
  61. public function template($code = '')
  62. {
  63. $this->_params['TemplateCode'] = $code;
  64. return $this;
  65. }
  66. /**
  67. * 接收手机
  68. * @param string $mobile 手机号码
  69. * @return Alisms
  70. */
  71. public function mobile($mobile = '')
  72. {
  73. $this->_params['PhoneNumbers'] = $mobile;
  74. return $this;
  75. }
  76. /**
  77. * 立即发送
  78. * @return boolean
  79. */
  80. public function send()
  81. {
  82. $this->error = '';
  83. $params = $this->_params();
  84. $params['Signature'] = $this->_signed($params);
  85. $response = $this->_curl($params);
  86. if ($response !== false) {
  87. $res = (array)json_decode($response, true);
  88. if (isset($res['Code']) && $res['Code'] == 'OK') {
  89. return true;
  90. }
  91. $this->error = isset($res['Message']) ? $res['Message'] : 'InvalidResult';
  92. } else {
  93. $this->error = 'InvalidResult';
  94. }
  95. return false;
  96. }
  97. /**
  98. * 获取错误信息
  99. * @return string
  100. */
  101. public function getError()
  102. {
  103. return $this->error;
  104. }
  105. private function _params()
  106. {
  107. return array_merge([
  108. 'AccessKeyId' => $this->config['key'],
  109. 'SignName' => isset($this->config['sign']) ? $this->config['sign'] : '',
  110. 'Action' => 'SendSms',
  111. 'Format' => 'JSON',
  112. 'Version' => '2017-05-25',
  113. 'SignatureVersion' => '1.0',
  114. 'SignatureMethod' => 'HMAC-SHA1',
  115. 'SignatureNonce' => uniqid(),
  116. 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
  117. ], $this->_params);
  118. }
  119. private function percentEncode($string)
  120. {
  121. $string = urlencode($string);
  122. $string = preg_replace('/\+/', '%20', $string);
  123. $string = preg_replace('/\*/', '%2A', $string);
  124. $string = preg_replace('/%7E/', '~', $string);
  125. return $string;
  126. }
  127. private function _signed($params)
  128. {
  129. $sign = $this->config['secret'];
  130. ksort($params);
  131. $canonicalizedQueryString = '';
  132. foreach ($params as $key => $value) {
  133. $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
  134. }
  135. $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  136. $signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true));
  137. return $signature;
  138. }
  139. private function _curl($params)
  140. {
  141. $uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
  142. $ch = curl_init();
  143. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  144. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  145. curl_setopt($ch, CURLOPT_URL, $uri);
  146. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  148. 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");
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  151. $reponse = curl_exec($ch);
  152. curl_close($ch);
  153. return $reponse;
  154. }
  155. }