1
votes

The script executes multiple URL's at once, and automatically echo the content of the URL, I want to prevent this script from echoing anything, but I don't see any line like echo 'something'; except the lines I commented out.

This is the script:


    function batch_execute($urls)
        {
            $i            = 0;
            $block        = array();
            $curl_handles = array();

            foreach ($urls as $url)
            {
                $curl_handles[$url] = curl_init();
                curl_setopt($curl_handles[$url], CURLOPT_URL, $url);
                curl_setopt($curl_handles[$url], CURLOPT_CONNECTTIMEOUT, 100);
            }

            $curl_multi_handle = curl_multi_init();

            foreach ($curl_handles as $a_curl_handle)
            {
                $i++;

                curl_multi_add_handle($curl_multi_handle, $a_curl_handle);

                $block[] = $a_curl_handle;

                if (($i % 10 == 0) or ($i == count($curl_handles)))
                {
                    $running = NULL;

                    do
                    {
                        $running_before = $running;

                        curl_multi_exec($curl_multi_handle, $running);

                        if ($running != $running_before)
                        {
    //                      echo("Waiting for $running sites to finish...\n");
                        }

                    } while ($running > 0);

                    foreach ($block as $handle)
                    {
    //                  $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

    //                  $curl_errno = curl_errno($handle);

                        $curl_error = curl_error($handle);

                        if ($curl_error)
                        {
    //                      echo("    *** cURL error: ($curl_errno) $curl_error\n");
                        }

                        curl_multi_remove_handle($curl_multi_handle, $handle);
                    }

                    $block = array();
                }
            }

            curl_multi_close($curl_multi_handle);
        }

Help :)

2
Try turning on the output buffer (ob_start()).Matt
I believe you need to set CURLOPT_NOBODY to true.Kermit
HEY @Matt thanks a lot :) it workedSanjana Thakur
Remember to turn off the output buffer when you're finished (and clean it). ob_end_clean()Thomas Clayson
Don't flush... clean! haha, flush will output to the browser. use ob_end_clean()Thomas Clayson

2 Answers

3
votes

Turning on the output buffer with ob_start() will capture all output before printing to the screen.

Then close the buffer with either ob_end_clean() or ob_end_flush(). Flush will print the contents of the buffer before closing it.

Take a look here for more information on output controls.

0
votes

When you are creating the individual cURL handles, set the option CURLOPT_RETURNTRANSFER to 1 and cURL will not output the contents to stdout as it is making the requests.

Once the request is complete, if you want to access the response body for an individual handle, call curl_multi_getcontent() on that handle and it will return the content of the response.

You can use CURLOPT_NOBODY, but the side effect is that the request is changed to a HEAD request which many servers may not accept.

Using output buffering is also a possibility, but isn't helpful if you need to isolate the response body from individual requests.