Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:27:08+00:00 2026-05-22T12:27:08+00:00

trying to make 5 curl childs for curl handler and defining them, but can’t

  • 0

trying to make 5 curl childs for curl handler and defining them, but can’t find the best way..my code so far

$curls = array($ch1, $ch2, $ch3, $ch4, $ch5); // have a bad feelin about this
$cont = array($cont1, $cont2, $cont3, $cont4, $cont5); // bad

for($i = 0; $i < count($curls); $i++) { // bad
    $curls[$i] = curl_init();

    curl_setopt($curls[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curls[$i], CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curls[$i], CURLOPT_REFERER, $ref);
    curl_setopt($curls[$i], CURLOPT_USERAGENT, $useragent);

    curl_setopt($curls[$i], CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curls[$i], CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($curls[$i], CURLOPT_URL, $url);
    curl_setopt($curls[$i], CURLOPT_POST, true);
    curl_setopt($curls[$i], CURLOPT_POSTFIELDS, $data);
    $cont[$i] = curl_exec($curls[$i]); //  bad

    curl_setopt($curls[$i], CURLOPT_URL, $url);
    curl_setopt($curls[$i], CURLOPT_POST, true);
    curl_setopt($curls[$i], CURLOPT_POSTFIELDS, $data);
    $cont[$i] = curl_exec($curls[$i]); // bad
}

AND LATER:

$mh = curl_multi_init();

curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
curl_multi_add_handle($mh,$ch3);
curl_multi_add_handle($mh,$ch4);
curl_multi_add_handle($mh,$ch5);

does this work or..is this optimal way? seems kinda bumpy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:27:08+00:00Added an answer on May 22, 2026 at 12:27 pm

    I would actually handle this a different way. I’d make a single function that handles the creation for a single instance of curl, with parameters to adjust the variable settings as necessary. This lets me create a single instance, or I can make a loop to create multiple instances.

    The thing is, many times multiple curl calls depend on whether or not the previous call succeeded. If the first one doesn’t succeed, I’ve now wastefully allocated multiple curl objects. Create the first, run it, error check, create the second, run it, error check it, etc. That way you’re only allocating what you need.

    Edit: Something like this

    // get the result of a single curl call
    function makeCurlCall($ref, $useragent, $cookiefile, $url, $data)
    {
        $curl = curl_init();
    
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    
        curl_setopt($curl, CURLOPT_REFERER, $ref);
        curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
    
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $cont = curl_exec($curl);
    
        // May need to use this later
        $error_no = curl_errno($curl);
    
        if($error_no) {
          // so we can close before we return
          $result = "[" . $error_no . "] " . curl_error($curl);
          curl_close($curl);
          return array('status' => 'error', 'result' => $result);
        }
        else {
          curl_close($curl);
          return array('status' => 'success', 'result' => $cont);
        }
    }
    
    $curl = makeCurlCall($ref, $useragent, $cookiefile, $url, $data);
    if($curl['status'] == 'error') {
      // do something for the error
    }
    else { 
      // do something with $curl['result']
    }
    
    // The first call worked, so make the next call, only allocating what we need
    $curl = makeCurlCall($ref, $useragent, $cookiefile, $url, $data);
    
    //etc.
    

    Note that you could probably include the functionality of handling the error and success if it’s generic enough, but you’ll still need to deal with the issue of a single curl call not working due to network issues, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
I'm trying to make the case for click-once and smart client development but my
I'm trying to make a request to a website this way : file_get_contents('http://www.yahoo.com/index.php?var=value'); in
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make a data bound column invisible after data binding, because it
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into
I am trying to make a JTable that has column spans available. Specifically, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.