SmsService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\Config;
  4. use app\common\model\MessageTemplate;
  5. use chuanglan\Chuanglan;
  6. use think\facade\Log;
  7. class SmsService
  8. {
  9. // public function send($mobile, $code, $content_param = [])
  10. // {
  11. // if (env('APP_DEBUG')) {
  12. // return ['code' => 0];
  13. // }
  14. //
  15. // $message = MessageTemplate::where('code', $code)->find();
  16. // if (empty($message)) {
  17. // Log::error('短信模板不存在:' . $code);
  18. // return ['code' => 1, 'msg' => '模板不存在'];
  19. // }
  20. //
  21. // $msg = $message['content'];
  22. // if (!empty($content_param)) {
  23. // foreach ($content_param as $k => $v) {
  24. // $msg = str_replace('{$' . ($k + 1) . '}', $v, $msg);
  25. // }
  26. // }
  27. // $sms = new Chuanglan();
  28. // $sms->send($mobile, ['message' => $msg]);
  29. //
  30. // return ['code' => 0];
  31. // }
  32. public function send($mobile, $code, $content_param = [])
  33. {
  34. $url = "https://lw_test.jinjianghc.com/api/sms/send";
  35. $token = "L1mq09OB68be4b4526092";
  36. // $url = "http://bd.lwtest.com/api/sms/send";
  37. // $token = "7ec987cb2daf4c44a84ff1f145c51f2b";
  38. $postArr = ['mobile'=>$mobile,'template_code'=>$code,'template_param'=>$content_param];
  39. $postFields = json_encode($postArr);
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  43. 'Content-Type: application/json; charset=utf-8', //json版本需要填写 Content-Type: application/json;
  44. 'token:'.$token
  45. ]
  46. );
  47. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt($ch, CURLOPT_POST, 1);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  51. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  54. $ret = curl_exec($ch);
  55. curl_close($ch);
  56. $rsp = json_decode($ret, true);
  57. if ($rsp['code'] != 0) {
  58. Log::record('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
  59. return ['code' => 1, 'msg' => $rsp['errorMsg']];
  60. } else {
  61. return ['code' => 0];
  62. }
  63. }
  64. public function examineSend($code, $content_param = [])
  65. {
  66. $mobile = Config::getConfigValue('examine_mobile');
  67. return $this->send($mobile, $code, $content_param);
  68. }
  69. }