Pouria P
Pouria P

Reputation: 595

multiple curl requests, when to close the handle?

I'm confused with curl's init() and close() functions. I want to know when should I close the curl handle in each of these situations:
1. using a single handle to get a "single" URL, with different options. for example:

$curl=curl_init('google.com');
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
curl_exec($curl);

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:

curl_setopt($curl,CURLOPT_FOLLOWLOCATION,false);
curl_exec($curl);

2.using a single handle to get "multiple" URLs. for example:

$curl = curl_init('google.com');
curl_exec($curl);

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:

$curl = curl_init('stackoverflow.com');
curl_exec($curl);

3.using a mulit handle to get mutltiple URLs. This is my code:

$CONNECTIONS=10;
$urls=fopen('urls.txt','r');
while(!feof($urls))
{
   $curl_array=array();
   $curl=curl_multi_init();
   for($i=0;$i<$CONNECTIONS&&!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>0);
   for($i=0;$i<$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);
}

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?
thanks

Upvotes: 6

Views: 4809

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72961

when to close the handle?

Simply put, you close it when you're done.

To address your specific questions:

  1. If you plan to reuse a curl handle (single resource), close it when you're done.
  2. If you have multiple curl handles (multiple resources), you can run them together with the curl_multi_* functions as you've shown, or run them sequentially and close the curl handle when you're done with that resource (see #1).
  3. This looks correct to me.

Note if you are reusing a curl handle, be mindful about things like caching, redirection, and other option settings that may affect multiple requests to the same resource.

I'd encourage you develop your code to request each resource once.

Upvotes: 4

Related Questions