php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77682 curl_multi_exec example unnecesary complicated
Submitted: 2019-02-28 17:30 UTC Modified: 2019-02-28 22:08 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: daniel at haxx dot se Assigned: salathe (profile)
Status: Closed Package: cURL related
PHP Version: master-Git-2019-02-28 (Git) OS: all
Private report: No CVE-ID: None
 [2019-02-28 17:30 UTC] daniel at haxx dot se
Description:
------------
The example code in the curl_multi_exec documentation is written to work with a very old libcurl version and is therefor much more complicated than it has to be with a libcurl release from the last nine years.

Before libcurl 7.20.0 (which was released on Feb 9 2010) it could return CURLM_CALL_MULTI_PERFORM and therefore (PHP) applications needed to consider that. *After* that release however, libcurl never returns that value.

I think now, there should be very few users around still with such an old libcurl in use (and those who do use such an old version has a busload of other worse problems to deal with) so I think it is time to reduce the complexity of that example. Mostly because so many users copy that example and run with it.

Suggested new example:

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

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
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;
$mrc = CURLM_OK;

while ($active && $mrc == CURLM_OK) {
    $mrc = curl_multi_exec($mh, $active);
    if($active) {
        curl_multi_select($mh);
    }
}

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

?>

Please bear with me. I'm the main curl author, I am not fluent in PHP. Feel free to edit and clarify the example further. My goal here was to remove the useless and confusing extra loops that are used in the existing documentation for this function.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-28 17:34 UTC] daniel at haxx dot se
Maybe it's better to do it:

do {
    $mrc = curl_multi_exec($mh, $active);
    if($active) {
        curl_multi_select($mh);
    }
} while ($active && $mrc == CURLM_OK);
 [2019-02-28 22:00 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346933
Log: improve curl_multi_exec() loops (doc #77682)

curl_multi_exec() hasn't returned CURLM_CALL_MULTI_PERFORM for a loooong
time (it stopped doing so as of libcurl 7.20.0, which was released on
Feb 9 2010).

Thanks to Daniel Stenberg for the details.
 [2019-02-28 22:06 UTC] salathe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: salathe
 [2019-02-28 22:06 UTC] salathe@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

I have updated the examples with loops like the do/while that you suggested, Daniel. Thanks for the push in the right direction!

There is a bunch of tidying up, clarifications, and so on, still to be done for the curl_multi_* functions. They are a common source of confusion and questions, for sure, and anything we can do to make the docs clearer is much needed.
 [2019-02-28 22:08 UTC] salathe@php.net
This is a docs change, not a php-src one. Sorry for the wrong "quick fix" text in my last reply. It should have said:

"This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better."

Followed by my "I have updated..." comments. *shakes fist at bug tracker*
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 09:01:26 2024 UTC