Sms.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace api\common;
  3. use app\talent\model\SmsLogModel;
  4. use think\facade\Log;
  5. class Sms
  6. {
  7. public function send($mobile, $code, $type = '人才便捷出行', $content_param = [])
  8. {
  9. $url = "https://lw_test.jinjianghc.com/api/sms/send";
  10. $token = "L1mq09OB68be4b4526092";
  11. // $url = "http://bd.lwtest.com/api/sms/send";
  12. // $token = "oYtEwqzL68be506f50a52";
  13. $postArr = ['mobile' => $mobile, 'template_code' => $code, 'template_param' => $content_param];
  14. $postFields = json_encode($postArr);
  15. $ch = curl_init();
  16. curl_setopt($ch, CURLOPT_URL, $url);
  17. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  18. 'Content-Type: application/json; charset=utf-8', //json版本需要填写 Content-Type: application/json;
  19. 'token:' . $token,
  20. ]
  21. );
  22. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24. curl_setopt($ch, CURLOPT_POST, 1);
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  26. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  27. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  28. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  29. $ret = curl_exec($ch);
  30. curl_close($ch);
  31. $rsp = json_decode($ret, true);
  32. $status = 1;
  33. if (empty($rsp['code'])) {
  34. $status = 2;
  35. }
  36. SmsLogModel::create([
  37. 'type' => $type,
  38. 'mobile' => $mobile,
  39. 'status' => $status,
  40. 'template_code' => $code,
  41. 'template_param' => json_encode($content_param),
  42. 'api_return' => $ret,
  43. 'create_time' => date('Y-m-d H:i:s'),
  44. ]);
  45. if ($rsp['code'] != 0) {
  46. Log::record('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
  47. return ['code' => 1, 'msg' => empty($rsp['errorMsg']) ? $rsp['msg'] : $rsp['errorMsg']];
  48. } else {
  49. return ['code' => 0];
  50. }
  51. }
  52. }