4

I have built a curl class that can download images in parallel using curl_multi_init.

The download function is below

public function download(AbstractRequest $request, $f) {

    // Initiate a new curl
    $ch = curl_init();
    // Set curl options
    curl_setopt_array($ch, [
        CURLOPT_URL => $request->getUrl(),
        CURLOPT_FILE => $f,
        CURLOPT_TIMEOUT => 99,
    ]);
    // Add to curl multi handle
    curl_multi_add_handle($this->multiCh, $ch);
}

The destructor for the class calls the execution of the parallel requests.

My question is, how can I get the file resource back from the curl_multi_exec in order to close it with fclose()?

Does curl automatically close the file handle for me?

Thanks

1 Answer 1

4
+100

Neither cURL nor PHP automatically close the file handle after the cURL request executes. PHP will close it automatically when the entire script terminates, but if the script continues to run for a while doing other things after the cURL requests complete, the files remain open.

All PHP does is flush file data to disk when the request completes.

You can easily test this with some simple code:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

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

// open two temp files for requests
$fp1 = fopen('/tmp/1.txt', 'w+');
$fp2 = fopen('/tmp/2.txt', 'w+');

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_FILE, $fp1);

curl_setopt($ch2, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_FILE, $fp2);

//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
echo "Begin requests\n";
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

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

echo "Done\n";

var_dump($fp1, $fp2); // valid resources

echo "Sleep\n";
sleep(10);

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

fwrite($fp1, "\n\nTEST MORE DATA\n");
fwrite($fp2, "\n\nAND MORE TEST DATA\n");

var_dump($fp1, $fp2); // valid file handles

echo "Last sleep\n";
sleep(10);

// data automatically flushed to disk here
// file handles will be closed by PHP when script terminates here

After running, you will see the extra data written to the end of those files, showing that the handles were still valid after cURL completed. Note: I ran these tests on PHP 7.0.5 and 5.6.20.

If you're feeling adventurous, you can dig into the PHP/cURL source and see for yourself. The CURLOPT_FILE handle is ch->handlers->write->fp throughout the file.

There is no way to get the file handle back so you'll have to keep track of them yourself.

It could be as simple as adding them all to an array:

$fileHandles = array();
//..
$fileHandles[] = $fp1;
$fileHandles[] = $fp2;

And then closing them all when curl_multi_exec completes:

foreach($fileHandles as $fp) {
    fclose($fp);
}

But the answer is, they will not be closed automatically by cURL or PHP until PHP terminates.

If this happens very shortly after cURL finishes, there's really no need to do it yourself. But if this is a long running script that repeats multi requests over and over for a long period of time, it might not be a bad idea to keep track of them and close them as the requests finish.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.