I have a function that calls 3 different APIs using cURL multiple times. Each API's result is passed to the next API called in nested loops, so cURL is currently opened and closed over 500 times.
Should I leave cURL open for the entire function or is it OK to open and close it so many times in one function?
The Curl client can tell the server that it wants to close the connection by sending the "Connection: close" header to the server. To pass the "Connection: close" header to Curl, you can use the -H command-line option.
curl_close() This function is used to close a cURL session.
curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.
There's a performance increase to reusing the same handle. See: Reusing the same curl handle. Big performance increase?
If you don't need the requests to be synchronous, consider using the curl_multi_* functions (e.g. curl_multi_init, curl_multi_exec, etc.) which also provide a big performance boost.
UPDATE:
I tried benching curl with using a new handle for each request and using the same handle with the following code:
ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); for ($i = 0; $i < 100; ++$i) { $rand = rand(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand); curl_exec($ch); curl_close($ch); } $end_time = microtime(true); ob_end_clean(); echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>'; ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); $ch = curl_init(); for ($i = 0; $i < 100; ++$i) { $rand = rand(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand); curl_exec($ch); } curl_close($ch); $end_time = microtime(true); ob_end_clean(); echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';
and got the following results:
Curl without handle reuse: 8.5690529346466 Curl with handle reuse: 5.3703031539917
So reusing the same handle actually provides a substantial performance increase when connecting to the same server multiple times. I tried connecting to different servers:
$url_arr = array( 'http://www.google.com/', 'http://www.bing.com/', 'http://www.yahoo.com/', 'http://www.slashdot.org/', 'http://www.stackoverflow.com/', 'http://github.com/', 'http://www.harvard.edu/', 'http://www.gamefaqs.com/', 'http://www.mangaupdates.com/', 'http://www.cnn.com/' ); ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); foreach ($url_arr as $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_exec($ch); curl_close($ch); } $end_time = microtime(true); ob_end_clean(); echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>'; ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); $ch = curl_init(); foreach ($url_arr as $url) { curl_setopt($ch, CURLOPT_URL, $url); curl_exec($ch); } curl_close($ch); $end_time = microtime(true); ob_end_clean(); echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';
And got the following result:
Curl without handle reuse: 3.7672290802002 Curl with handle reuse: 3.0146431922913
Still quite a substantial performance increase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With