curl_exec: how to cancel a long running HTTP request?
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 开发者_Python百科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?
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);
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.
精彩评论