Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Using multithreading with curl in PHP

Hello there, I am building a console application in php which allows user to fetch some pdf files from urls, say i have 100 urls. I have to fetch those pdf files and merge them in a single file. Time for fetching those 100 pdf's must be less than 10 seconds. But my program is taking 30-40 seconds to fetch those files. I think multithreading is a way to fetch files in required time. But as i am new to php, i am not getting how to use multithreading with curl. Any help on that would be appreciated. Here is my code.


$file = fopen("filenames.txt", 'r');
    $fileArr = explode("\n", file_get_contents('filenames.txt'));
echo "Started fetching at ". date("h:i:sa");
for ($i = 0; $i<count($fileArr); $i++){
    $url = "https://images.s3.amazonaws.com/PdfFiles/".$fileArr[$i];
    $path = "path/file".$i.".pdf"; 
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $result = file_put_contents($path, $data);
            echo "error\n";
            echo "success\n";
echo "fetched all files at ". date("h:i:sa");


Any help would be appreciated.