Test.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Log;
  4. class Test
  5. {
  6. public function t1()
  7. {
  8. if (env('APP_DEBUG')) {
  9. return ['code' => 0];
  10. }
  11. $url = "https://lw_test.jinjianghc.com/api/sms/send";
  12. $token = "oYtEwqzL68be506f50a52";
  13. // $url = "http://bd.lwtest.com/api/sms/send";
  14. // $token = "7ec987cb2daf4c44a84ff1f145c51f2b";
  15. $postArr = ['mobile'=>'13313826760','template_code'=>'verification_code','template_param'=>['code'=>'123456']];
  16. $postFields = json_encode($postArr);
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  20. 'Content-Type: application/json; charset=utf-8', //json版本需要填写 Content-Type: application/json;
  21. 'token:'.$token
  22. ]
  23. );
  24. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_POST, 1);
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  28. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  29. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  30. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  31. $ret = curl_exec($ch);
  32. curl_close($ch);
  33. $rsp = json_decode($ret, true);
  34. halt($rsp);
  35. if ($rsp['code'] != 0) {
  36. Log::record('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
  37. return ['code' => 1, 'msg' => $rsp['errorMsg']];
  38. } else {
  39. return ['code' => 0];
  40. }
  41. }
  42. }