0

I'm having an issue with Curl Multi Init. I'm trying to have it visit multiple sites at the same time and then save their content to variables. Unfortunately, I can't figure out why it seems to be echoing the content of each site. For example, if I tell it to go to two sites, one of the site's content being "hello" and and another being "hey" it would echo "hellohey" -- not sure why this is happening. Here is the code I am using:

    <?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://example1.org/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://example2.org/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) == -1) {
        usleep(100);
    }
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>
Grant
  • 229
  • 5
  • 13

1 Answers1

4

All right. You didn't set CURLOPT_RETURNTRANSFER to true: return the transfer as a string without passing to stdout (curl-setopt#CURLOPT_RETURNTRANSFER). Changes:

 curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
 curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1 );

For saving their content to variables, you should use curl-multi-getcontent.

voodoo417
  • 11,861
  • 3
  • 36
  • 40