Doc #77682 [Com]: curl_multi_exec example unnecesary complicated
| From: | daniel at haxx dot se | Date: | Thu, 28 Feb 2019 17:34:29 +0000 |
| Subject: | Doc #77682 [Com]: curl_multi_exec example unnecesary complicated | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16439@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77682&edit=1
ID: 77682
Comment by: daniel at haxx dot se
Reported by: daniel at haxx dot se
Summary: curl_multi_exec example unnecesary complicated
Status: Open
Type: Documentation Problem
Package: cURL related
Operating System: all
PHP Version: master-Git-2019-02-28 (Git)
Block user comment: N
Private report: N
New Comment:
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);
Previous Comments:
------------------------------------------------------------------------
[2019-02-28 17:30:31] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77682&edit=1