curl_exec:如何取消长时间运行的 HTTP 请求?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
CURLOPT_TIMEOUT
。来自手册:You could use
CURLOPT_TIMEOUT
. From the manual:根据远程服务器的配置方式,该过程可能会在您断开连接后立即结束。除非您分叉,或者远程服务器配置为允许它,否则您将不得不等待它结束。
如果您可以控制该远程服务器,则可以配置 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.