laravel如何处理第三方API Http::post错误



im new in laravel im使用第三方API发送短信通知,但第三方API有一些宕机时间,所以我需要刷新页面一次或两次才能发送通知,我的网站上出现错误。

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

获取时出错

IlluminateHttpClientConnectionException
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);
}

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

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

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

因此,以上操作将重试3次,请求间隔30秒。

你可能不想抛出错误,但你不能永远重试,所以我建议使用trycatch块,这样应用程序就可以正常失败。错误处理是一件大事。

最新更新