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

4
  • Why are you executing the handles in the loop where you're defining them? Isn't the point of multi handles to do it in parallel? Commented May 15, 2011 at 19:07
  • @Tim Yates: well they are not doin parallel handles yet, they are loggig in one by one, and when they are all logged in, then they start to do parallel jobs Commented May 15, 2011 at 19:27
  • Do you need to login to a different account or each? Couldn't you just share the cookie file after one login? Commented May 15, 2011 at 20:11
  • $Ryan Pendelton: same account with all 5 curl childs, share cookie file? how? Commented May 15, 2011 at 20:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you add some code with that ? im not quite sure what you mean
0

You could try something like this. I'm not sure if it would work in your scenario, but it's a start.

function makeCurl($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);

    if($data){
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    return $curl;
}

// first let's login

$login = makeCurl($loginref, $useragent, $cookiefile, $loginurl, $logindata);
curl_exec($login);
curl_close($login);

// $cookiefile now has the needed cookies
// for the parallel jobs to run

$curls = array();
$mh = curl_multi_init();

$curls[0] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[1] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[2] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[3] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[4] = makeCurl($ref, $useragent, $cookiefile, $url, $data);
$curls[5] = makeCurl($ref, $useragent, $cookiefile, $url, $data);

for($i = 0; $i < count($curls); $i++){
    curl_multi_add_handle($mh, $curls[$i]);
}

// use curl_multi_exec to run them and wait for them to finish
// (I've never done this before, so i can't tell you how to do it)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.