Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.