Sms.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * 手机短信类
  4. */
  5. namespace sendmsg;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use Qcloud\Sms\SmsSingleSender;
  8. use think\facade\Log;
  9. class Sms
  10. {
  11. /*
  12. * 发送手机短信
  13. * @param unknown $mobile 手机号
  14. * @param unknown $smslog_param 短信参数
  15. */
  16. public function send($mobile, $smslog_param)
  17. {
  18. if (config('ds_config.smscf_type') == 'wj') {
  19. $content = $smslog_param['message'];
  20. return $this->mysend_sms($mobile, $content);
  21. } elseif (config('ds_config.smscf_type') == 'ali') {
  22. return $this->ali_send($mobile, $smslog_param);
  23. } elseif (config('ds_config.smscf_type') == 'ten') {
  24. return $this->ten_send($mobile, $smslog_param);
  25. } elseif (config('ds_config.smscf_type') == 'chuanglan') {
  26. return $this->cl_send($mobile, $smslog_param);
  27. } else {
  28. return ds_callback(false, lang('param_error'));
  29. }
  30. }
  31. /*
  32. 您于{$send_time}绑定手机号,验证码是:{$verify_code}。【{$site_name}】
  33. -1 没有该用户账户
  34. -2 接口密钥不正确 [查看密钥]不是账户登陆密码
  35. -21 MD5接口密钥加密不正确
  36. -3 短信数量不足
  37. -11 该用户被禁用
  38. -14 短信内容出现非法字符
  39. -4 手机号格式不正确
  40. -41 手机号码为空
  41. -42 短信内容为空
  42. -51 短信签名格式不正确接口签名格式为:【签名内容】
  43. -6 IP限制
  44. 大于0 短信发送数量
  45. http://utf8.api.smschinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
  46. */
  47. public function mysend_sms($mobile, $content)
  48. {
  49. $user_id = urlencode(config('ds_config.smscf_wj_username')); // 这里填写用户名
  50. $key = urlencode(config('ds_config.smscf_wj_key')); // 这里填接口安全密钥
  51. if (!$mobile || !$content || !$user_id || !$key)
  52. return false;
  53. if (is_array($mobile)) {
  54. $mobile = implode(",", $mobile);
  55. }
  56. $mobile = urlencode($mobile);
  57. $content = urlencode($content);
  58. $url = "http://utf8.api.smschinese.cn/?Uid=" . $user_id . "&Key=" . $key . "&smsMob=" . $mobile . "&smsText=" . $content;
  59. if (function_exists('file_get_contents')) {
  60. $res = file_get_contents($url);
  61. } else {
  62. $ch = curl_init();
  63. $timeout = 5;
  64. curl_setopt($ch, CURLOPT_URL, $url);
  65. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  66. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  67. $res = curl_exec($ch);
  68. curl_close($ch);
  69. }
  70. //短信发送后返回值 说明
  71. $message = '';
  72. switch ($res) {
  73. case "-1":
  74. $message = '没有该用户账号';
  75. break;
  76. case "-2":
  77. $message = '接口密钥不正确 [查看密钥]不是账户登陆密码';
  78. break;
  79. case "-21":
  80. $message = 'MD5接口密钥加密不正确';
  81. break;
  82. case "-3":
  83. $message = '短信数量不足';
  84. break;
  85. case "-11":
  86. $message = '该用户被禁用';
  87. break;
  88. case "-14":
  89. $message = '短信内容出现非法字符';
  90. break;
  91. case "-4":
  92. $message = '手机号格式不正确';
  93. break;
  94. case "-41":
  95. $message = '手机号码为空';
  96. break;
  97. case "-42":
  98. $message = '短信内容为空';
  99. break;
  100. case "-51":
  101. $message = '短信签名格式不正确接口签名格式为:【签名内容】';
  102. break;
  103. case "-6":
  104. $message = 'IP限制';
  105. break;
  106. }
  107. if ($res > 0) {
  108. return ds_callback(true);
  109. } else {
  110. return ds_callback(false, $message);
  111. }
  112. }
  113. public function ali_send($mobile, $smslog_param)
  114. {
  115. if (!$smslog_param['ali_template_code'] || !config('ds_config.smscf_ali_id') || !config('ds_config.smscf_ali_secret') || !config('ds_config.smscf_sign')) {
  116. return ds_callback(false, '请绑定模板code');
  117. }
  118. AlibabaCloud::accessKeyClient(config('ds_config.smscf_ali_id'), config('ds_config.smscf_ali_secret'))
  119. ->regionId('cn-hangzhou')
  120. ->asDefaultClient();
  121. try {
  122. $result = AlibabaCloud::rpc()
  123. ->product('Dysmsapi')
  124. // ->scheme('https') // https | http
  125. ->version('2017-05-25')
  126. ->action('SendSms')
  127. ->method('POST')
  128. ->host('dysmsapi.aliyuncs.com')
  129. ->options([
  130. 'query' => [
  131. 'RegionId' => "cn-hangzhou",
  132. 'PhoneNumbers' => $mobile,
  133. 'SignName' => config('ds_config.smscf_sign'),
  134. 'TemplateCode' => $smslog_param['ali_template_code'],
  135. 'TemplateParam' => json_encode($smslog_param['ali_template_param']),
  136. ],
  137. ])
  138. ->request();
  139. } catch (\Exception $e) {
  140. return ds_callback(false, $e->getMessage());
  141. }
  142. $result = $result->toArray();
  143. if ($result['Code'] != 'OK') {
  144. return ds_callback(false, $result['Message']);
  145. } else {
  146. return ds_callback(true);
  147. }
  148. }
  149. /**
  150. * 腾讯云接口
  151. * @param $mobile
  152. * @param $smslog_param
  153. * @return \multitype
  154. */
  155. public function ten_send($mobile, $smslog_param)
  156. {
  157. if (!$smslog_param['ten_template_code'] || !config('ds_config.smscf_ten_id') || !config('ds_config.smscf_ten_secret') || !config('ds_config.smscf_sign')) {
  158. return ds_callback(false, '请绑定模板code');
  159. }
  160. // 短信应用 SDK AppID
  161. $appid = config('ds_config.smscf_ten_id'); // SDK AppID 以1400开头
  162. // 短信应用 SDK AppKey
  163. $appkey = config('ds_config.smscf_ten_secret');
  164. // 需要发送短信的手机号码
  165. $phoneNumbers = $mobile;
  166. // 短信模板 ID,需要在短信控制台中申请
  167. $templateId = $smslog_param['ten_template_code']; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  168. $smsSign = config('ds_config.smscf_sign'); // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请
  169. try {
  170. $sender = new SmsSingleSender($appid, $appkey);
  171. $params = $smslog_param['ten_template_param'];//生成随机数
  172. $result = $sender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", "");
  173. $rsp = json_decode($result);
  174. } catch (\Exception $e) {
  175. return ds_callback(false, $e->getMessage());
  176. }
  177. if ($rsp->errmsg != 'OK') {
  178. return ds_callback(false, $rsp->errmsg);
  179. } else {
  180. return ds_callback(true);
  181. }
  182. }
  183. /**
  184. * 创蓝短信
  185. * @param $mobile
  186. * @param $smslog_param
  187. * @return \multitype
  188. */
  189. public function cl_send($mobile, $smslog_param)
  190. {
  191. if (!config('ds_config.smscf_chuanglan_appkey') || !config('ds_config.smscf_chuanglan_secret')) {
  192. return ds_callback(false, '请绑定模板参数');
  193. }
  194. if (empty($smslog_param['params'])) {
  195. $url = 'http://smssh1.253.com/msg/send/json';
  196. //创蓝接口参数
  197. $postArr = [
  198. 'account' => config('ds_config.smscf_chuanglan_appkey'),
  199. 'password' => config('ds_config.smscf_chuanglan_secret'),
  200. 'msg' => $smslog_param['message'],
  201. 'phone' => $mobile,
  202. 'report' => 'true',
  203. ];
  204. } else {
  205. $params_str = $mobile . ',' . implode(',', $smslog_param['params']);
  206. $url = 'http://smssh1.253.com/msg/variable/json';
  207. //创蓝接口参数
  208. $postArr = [
  209. 'account' => config('ds_config.smscf_chaunglan_appkey'),
  210. 'password' => config('ds_config.smscf_chaunglan_secret'),
  211. 'msg' => $smslog_param['message'],
  212. 'params' => $params_str,
  213. 'report' => 'true',
  214. ];
  215. }
  216. $postFields = json_encode($postArr);
  217. $ch = curl_init();
  218. curl_setopt($ch, CURLOPT_URL, $url);
  219. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  220. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  221. ]
  222. );
  223. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  224. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  225. curl_setopt($ch, CURLOPT_POST, 1);
  226. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  227. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  228. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  229. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  230. $ret = curl_exec($ch);
  231. curl_close($ch);
  232. $rsp = json_decode($ret, true);
  233. if ($rsp['code'] != 0) {
  234. Log::record('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
  235. return ds_callback(false, $rsp['errorMsg']);
  236. } else {
  237. return ds_callback(true);
  238. }
  239. }
  240. }