Reputation:
I am trying to retrieve some data from an API using CURL request. Is it possible to track the time passed from request start and stop the request after sometime, may be less then the timeout set to it?
NOTE:
My aim is to not set timeout. Let the request continue until another job/function call don't arise. Is it possible?
In Detail:
What I exactly want is that, I have a function that call through Ajax and then a CURL start, and another Ajax will also call that function with some specific parameter, when second ajax call happen then the execution of CURL should stop. But the time interval within these two call is arbitrary.
Upvotes: 2
Views: 5753
Reputation: 37065
I'm not sure what you are using to determine when enough is enough, but the following will stop a curl download in it's tracks:
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($ch, "downloader"));
downloader
just being a random function name, it takes the curl resource and the function name to pass the received input for saving. It has to return the length received, or the connection aborts, so if you didn't want that to happen, you'd have the following:
function downloader($curlHandle,$data)
{
$data_string .= $data; // Store your data for later.
$data_length = strlen($data); // Get length of current chunk
return $data_length; // pass it back and keep going.
}
Now, hypothetically, if you had a global variable that indicated "Stop curl!" you could return a false size and abort the transfer. Something like:
function downloader($curlHandle,$data)
{
$data_string .= $data; // Store your data for later.
$data_length = strlen($data); // Get length of current chunk
global $stop_curl;
return ($stop_curl) ? "" : $data_length;
}
Upvotes: 2
Reputation: 318708
what I exactly want is that, I have a function that call through Ajax and then a CURL start, and another Ajax will also call that function with some specific parameter, when second ajax call happen then the execution of CURL should stop
Then you need to do that in your JavaScript code. Simply .abort()
the previous AJAX request before sending a new one.
Upvotes: 1
Reputation: 822
Use can use CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT for setting cURL options via curl_setopt() function as follows:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// The number of seconds to wait while trying to connect.
// Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_TIMEOUT, 10)
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Upvotes: 1
Reputation: 416
Simple way to getting script execution time in PHP like that:
function microtime_float()
{
list($utime, $time) = explode(" ", microtime());
return ((float)$utime + (float)$time);
}
$script_start = microtime_float();
// here your curl request start
....
// here your curl request stop
$script_end = microtime_float();
echo "Script executed in ".bcsub($script_end, $script_start, 4)." seconds.";
Upvotes: -2
Reputation: 17030
You can define that by setting CURLOPT_CONNECTTIMEOUT
and CURLOPT_TIMEOUT
options of cURL transfer (php doc)
Upvotes: 3