Alisms.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace fast\service;
  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 = Config::get('service.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['sms_free_sign_name'] = $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['sms_param'] = 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['sms_template_code'] = $code;
  66. return $this;
  67. }
  68. /**
  69. * 接收手机
  70. * @param string $mobile 手机号码
  71. * @return Alisms
  72. */
  73. public function mobile($mobile = '')
  74. {
  75. $this->_params['rec_num'] = $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['sign'] = $this->_signed($params);
  87. $reponse = $this->_curl($params);
  88. if ($reponse !== FALSE)
  89. {
  90. $res = json_decode($reponse, TRUE);
  91. $res = array_pop($res);
  92. if (isset($res['result']))
  93. return TRUE;
  94. $this->error = $res;
  95. }
  96. else
  97. {
  98. $this->error = array('code' => 0, 'msg' => 'HTTP_RESPONSE_NOT_WELL_FORMED');
  99. }
  100. return FALSE;
  101. }
  102. /**
  103. * 获取错误信息
  104. * @return array
  105. */
  106. public function getError()
  107. {
  108. return $this->error;
  109. }
  110. private function _params()
  111. {
  112. return array_merge([
  113. 'app_key' => $this->config['key'],
  114. 'format' => 'json',
  115. 'method' => 'alibaba.aliqin.fc.sms.num.send',
  116. 'v' => '2.0',
  117. 'timestamp' => date('Y-m-d H:i:s'),
  118. 'sign_method' => 'md5',
  119. 'sms_type' => 'normal'
  120. ], $this->_params);
  121. }
  122. private function _signed($params)
  123. {
  124. ksort($params);
  125. $sign = $this->config['secret'];
  126. foreach ($params as $k => $v)
  127. {
  128. if (is_string($v) && '@' != substr($v, 0, 1))
  129. $sign .= $k . $v;
  130. }
  131. $sign .= $this->config['secret'];
  132. return strtoupper(md5($sign));
  133. }
  134. private function _curl($params)
  135. {
  136. $uri = 'https://eco.taobao.com/router/rest?' . http_build_query($params);
  137. $ch = curl_init();
  138. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  139. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  140. curl_setopt($ch, CURLOPT_URL, $uri);
  141. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  142. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  143. 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");
  144. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  145. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  146. $reponse = curl_exec($ch);
  147. curl_close($ch);
  148. return $reponse;
  149. }
  150. }