Note that there are some explanatory texts on larger screens.

plurals
  1. POmultiple curl requests, when to close the handle?
    text
    copied!<p>I'm confused with curl's <code>init()</code> and <code>close()</code> functions. I want to know when should I close the curl handle in each of these situations:<br> 1. using a single handle to get a "single" URL, with different options. for example:<br></p> <pre><code>$curl=curl_init('google.com'); curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true); curl_exec($curl); </code></pre> <p>now i want to set FOLLOWLOCATION to false. should i do curl_close($curl), and do everything from the beginning, or just set the option and execute it again like this:<br></p> <pre><code>curl_setopt($curl,CURLOPT_FOLLOWLOCATION,false); curl_exec($curl); </code></pre> <p>2.using a single handle to get "multiple" URLs. for example:</p> <pre><code>$curl = curl_init('google.com'); curl_exec($curl); </code></pre> <p>now i want to get stackoverflow.com. should i close the handle and start from the beginning or i can set another URL without closing the handle. like this:</p> <pre><code>$curl = curl_init('stackoverflow.com'); curl_exec($curl); </code></pre> <p>3.using a mulit handle to get mutltiple URLs. This is my code:</p> <pre><code>$CONNECTIONS=10; $urls=fopen('urls.txt','r'); while(!feof($urls)) { $curl_array=array(); $curl=curl_multi_init(); for($i=0;$i&lt;$CONNECTIONS&amp;&amp;!feof($urls);$i++) //create 10 connections unless we have reached end of file { $url=fgets($urls); //get next url from file $curl_array[$i]=curl_init($url); curl_multi_add_handle($curl,$curl_array[$i]); } $running=NULL; do { curl_multi_exec($curl,$running); }while($running&gt;0); for($i=0;$i&lt;$CONNECTIONS;$i++) { $response=curl_multi_getcontent($curl_array[$i]); curl_multi_remove_handle($curl,$curl_array[$i]); curl_close(($curl_array[$i])); } curl_multi_close($curl); } </code></pre> <p>As you can see: after getting the contents of each single handle, I'm removing the handle from the multi handle, closing the single handle, and then closing the multi handle after the for loop. Is this practice correct or I am OVERCLOSING the handles?<br> thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload