Sms.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 手机短信类
  4. */
  5. namespace sendmsg;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use Qcloud\Sms\SmsSingleSender;
  8. class Sms
  9. {
  10. /*
  11. * 发送手机短信
  12. * @param unknown $mobile 手机号
  13. * @param unknown $smslog_param 短信参数
  14. */
  15. public function send($mobile, $smslog_param)
  16. {
  17. if(config('ds_config.smscf_type')=='wj'){
  18. $content=$smslog_param['message'];
  19. return $this->mysend_sms($mobile, $content);
  20. }elseif(config('ds_config.smscf_type')=='ali'){
  21. return $this->ali_send($mobile, $smslog_param);
  22. }elseif(config('ds_config.smscf_type')=='ten'){
  23. return $this->ten_send($mobile, $smslog_param);
  24. }else{
  25. return ds_callback(false,lang('param_error'));
  26. }
  27. }
  28. /*
  29. 您于{$send_time}绑定手机号,验证码是:{$verify_code}。【{$site_name}】
  30. -1 没有该用户账户
  31. -2 接口密钥不正确 [查看密钥]不是账户登陆密码
  32. -21 MD5接口密钥加密不正确
  33. -3 短信数量不足
  34. -11 该用户被禁用
  35. -14 短信内容出现非法字符
  36. -4 手机号格式不正确
  37. -41 手机号码为空
  38. -42 短信内容为空
  39. -51 短信签名格式不正确接口签名格式为:【签名内容】
  40. -6 IP限制
  41. 大于0 短信发送数量
  42. http://utf8.api.smschinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
  43. */
  44. public function mysend_sms($mobile, $content)
  45. {
  46. $user_id = urlencode(config('ds_config.smscf_wj_username')); // 这里填写用户名
  47. $key = urlencode(config('ds_config.smscf_wj_key')); // 这里填接口安全密钥
  48. if (!$mobile || !$content || !$user_id || !$key)
  49. return false;
  50. if (is_array($mobile)) {
  51. $mobile = implode(",", $mobile);
  52. }
  53. $mobile=urlencode($mobile);
  54. $content=urlencode($content);
  55. $url = "http://utf8.api.smschinese.cn/?Uid=" . $user_id . "&Key=" . $key . "&smsMob=" . $mobile . "&smsText=" . $content;
  56. if (function_exists('file_get_contents')) {
  57. $res = file_get_contents($url);
  58. }
  59. else {
  60. $ch = curl_init();
  61. $timeout = 5;
  62. curl_setopt($ch, CURLOPT_URL, $url);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  64. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  65. $res = curl_exec($ch);
  66. curl_close($ch);
  67. }
  68. //短信发送后返回值 说明
  69. $message = '';
  70. switch ($res){
  71. case "-1":
  72. $message = '没有该用户账号';
  73. break;
  74. case "-2":
  75. $message = '接口密钥不正确 [查看密钥]不是账户登陆密码';
  76. break;
  77. case "-21":
  78. $message = 'MD5接口密钥加密不正确';
  79. break;
  80. case "-3":
  81. $message = '短信数量不足';
  82. break;
  83. case "-11":
  84. $message = '该用户被禁用';
  85. break;
  86. case "-14":
  87. $message = '短信内容出现非法字符';
  88. break;
  89. case "-4":
  90. $message = '手机号格式不正确';
  91. break;
  92. case "-41":
  93. $message = '手机号码为空';
  94. break;
  95. case "-42":
  96. $message = '短信内容为空';
  97. break;
  98. case "-51":
  99. $message = '短信签名格式不正确接口签名格式为:【签名内容】';
  100. break;
  101. case "-6":
  102. $message = 'IP限制';
  103. break;
  104. }
  105. if($res>0){
  106. return ds_callback(true);
  107. }else{
  108. return ds_callback(false,$message);
  109. }
  110. }
  111. public function ali_send($mobile, $smslog_param) {
  112. if(!$smslog_param['ali_template_code'] || !config('ds_config.smscf_ali_id') || !config('ds_config.smscf_ali_secret') || !config('ds_config.smscf_sign')){
  113. return ds_callback(false,'请绑定模板code');
  114. }
  115. AlibabaCloud::accessKeyClient(config('ds_config.smscf_ali_id'), config('ds_config.smscf_ali_secret'))
  116. ->regionId('cn-hangzhou')
  117. ->asDefaultClient();
  118. try {
  119. $result = AlibabaCloud::rpc()
  120. ->product('Dysmsapi')
  121. // ->scheme('https') // https | http
  122. ->version('2017-05-25')
  123. ->action('SendSms')
  124. ->method('POST')
  125. ->host('dysmsapi.aliyuncs.com')
  126. ->options([
  127. 'query' => [
  128. 'RegionId' => "cn-hangzhou",
  129. 'PhoneNumbers' => $mobile,
  130. 'SignName' => config('ds_config.smscf_sign'),
  131. 'TemplateCode' => $smslog_param['ali_template_code'],
  132. 'TemplateParam' => json_encode($smslog_param['ali_template_param']),
  133. ],
  134. ])
  135. ->request();
  136. } catch (\Exception $e) {
  137. return ds_callback(false,$e->getMessage());
  138. }
  139. $result=$result->toArray();
  140. if($result['Code']!='OK'){
  141. return ds_callback(false,$result['Message']);
  142. }else{
  143. return ds_callback(true);
  144. }
  145. }
  146. /**
  147. * 腾讯云接口
  148. * @param $mobile
  149. * @param $smslog_param
  150. * @return \multitype
  151. */
  152. public function ten_send($mobile, $smslog_param) {
  153. if(!$smslog_param['ten_template_code'] || !config('ds_config.smscf_ten_id') || !config('ds_config.smscf_ten_secret') || !config('ds_config.smscf_sign')){
  154. return ds_callback(false,'请绑定模板code');
  155. }
  156. // 短信应用 SDK AppID
  157. $appid = config('ds_config.smscf_ten_id'); // SDK AppID 以1400开头
  158. // 短信应用 SDK AppKey
  159. $appkey = config('ds_config.smscf_ten_secret');
  160. // 需要发送短信的手机号码
  161. $phoneNumbers = $mobile;
  162. // 短信模板 ID,需要在短信控制台中申请
  163. $templateId = $smslog_param['ten_template_code']; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  164. $smsSign = config('ds_config.smscf_sign'); // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请
  165. try {
  166. $sender = new SmsSingleSender($appid, $appkey);
  167. $params = $smslog_param['ten_template_param'];//生成随机数
  168. $result = $sender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", "");
  169. $rsp = json_decode($result);
  170. } catch(\Exception $e) {
  171. return ds_callback(false,$e->getMessage());
  172. }
  173. if($rsp->errmsg!='OK'){
  174. return ds_callback(false,$rsp->errmsg);
  175. }else{
  176. return ds_callback(true);
  177. }
  178. }
  179. }