【问题标题】:laravel how to handle third party API Http::post errorlaravel 如何处理第三方 API Http::post 错误
【发布时间】:2022-06-15 06:44:51
【问题描述】:

我是 laravel 中的新手,我使用第三方 API 发送短信通知,但第三方 API 有一些停机时间,所以我需要刷新页面一次或两次来发送通知,那时我的网站上出现错误。

如果出现错误,我需要自动发送第二次尝试,并且我没有在我的网站上显示什么错误。

出现错误

Illuminate\Http\Client\ConnectionException
cURL error 28: Failed to connect to api.gateway.in port 80: Timed out (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

我的控制器

public function Store_Enrollment(Request $request)

    {

      $this->validate($request, [

  'student_name' => 'required|string|max:255',
  'phone_no' => 'required|string|max:10',
         
    ]);
 
   $input['student_name'] = ucfirst ($request['student_name']);
   $input['phone_no'] = $request->phone_no;
   $redirect = Student::create($input); 
 
  
Http::post("http://api.gateway.in/sendmessage.php?user=XXX&password=XXX&mobile=$redirect->phone_no&message=thank you $redirect->name,"); 

 return redirect('home' . thank you);

}

【问题讨论】:

    标签: laravel laravel-5 eloquent laravel-7 laravel-6


    【解决方案1】:

    HTTP 客户端有一个retry 函数,因此您可以执行以下操作:

    Http::retry(3, 30)->post("some_stuff");
    

    其中第一个参数是重试次数,第二个是重试之间的秒数,这是可选的。

    因此,上述请求将重试 3 次,请求之间的间隔为 30 秒。

    你可能不想抛出错误,但你不能永远回退,所以我建议使用 trycatch 块,这样应用程序就可以正常失败。错误处理很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 2020-08-11
      • 1970-01-01
      • 2018-12-06
      • 2020-11-14
      • 2011-07-10
      • 2018-07-02
      • 2022-01-09
      相关资源
      最近更新 更多