curl_exec:如何取消长时间运行的 HTTP 请求?

发布于 2024-12-09 21:05:09 字数 476 浏览 0 评论 0原文

我正在使用 PHP 的curl_exec() 向一个运行时间很长的API 发出请求。但我实际上不需要知道结果,我只需要开始这个过程。

该过程成功或失败对我来说并不重要。所以我想在发出请求后立即断开连接。

如何删除curl_exec连接?我不想等待 30 - 60 秒的响应:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://example.com/LongRunningProcess/parameter");        
curl_exec($ch); 
// need to do something here to just drop the connection, don't wait for curl_exec
// but what is that something?
curl_close($ch);

我相信解决方案可能会涉及curl_multi?

I'm using PHP's curl_exec() to make a request to an API that has a very long running process. But I don't actually need to know the result, I only need the process to be started.

It doesn't matter to me if the process succeeds or fails. So I want to drop the connection as soon as I have made the request.

How can I drop the curl_exec connection? I don't want to wait 30 - 60 seconds for the response:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://example.com/LongRunningProcess/parameter");        
curl_exec($ch); 
// need to do something here to just drop the connection, don't wait for curl_exec
// but what is that something?
curl_close($ch);

I believe that the solution will probably involve curl_multi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-12-16 21:05:09

您可以使用CURLOPT_TIMEOUT。来自手册

允许 cURL 函数执行的最大秒数。

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

You could use CURLOPT_TIMEOUT. From the manual:

The maximum number of seconds to allow cURL functions to execute.

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
孤星 2024-12-16 21:05:09

根据远程服务器的配置方式,该过程可能会在您断开连接后立即结束。除非您分叉,或者远程服务器配置为允许它,否则您将不得不等待它结束。

如果您可以控制该远程服务器,则可以配置 PHP 以使其继续运行。您正在寻找 ignore_user_abort 在 PHP.ini中,或作为函数。

Depending on how the remote server is configured, it is likely that the process will end as soon as you disconnect. Unless you fork, or the remote server is configured to allow it, you will have to wait for it to end.

If you have control over that remote server, you can configure PHP so that it will keep running. You're looking for ignore_user_abort in PHP.ini, or as a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文