reference/curl/functions/curl-multi-exec.xml
2ebb9660d47e648a28007960bf059d1f691c7f21
2ebb9660d47e648a28007960bf059d1f691c7f21
...
...
@@ -10,7 +10,7 @@
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>int</type><methodname>curl_multi_exec</methodname>
13
-
<methodparam><type>resource</type><parameter>mh</parameter></methodparam>
13
+
<methodparam><type>CurlMultiHandle</type><parameter>multi_handle</parameter></methodparam>
14
14
<methodparam><type>int</type><parameter role="reference">still_running</parameter></methodparam>
15
15
</methodsynopsis>
16
16
<para>
...
...
@@ -50,52 +50,84 @@
50
50
</note>
51
51
</refsect1>
52
52
53
+
<refsect1 role="changelog">
54
+
&reftitle.changelog;
55
+
<informaltable>
56
+
<tgroup cols="2">
57
+
<thead>
58
+
<row>
59
+
<entry>&Version;</entry>
60
+
<entry>&Description;</entry>
61
+
</row>
62
+
</thead>
63
+
<tbody>
64
+
&curl.changelog.multi-handle-param;
65
+
</tbody>
66
+
</tgroup>
67
+
</informaltable>
68
+
</refsect1>
69
+
53
70
<refsect1 role="examples">
54
71
&reftitle.examples;
55
72
<para>
56
73
<example>
57
74
<title><function>curl_multi_exec</function> example</title>
58
75
<para>
59
-
This example will create two cURL handles, add them to a multi
60
-
handle, and then run them in parallel.
76
+
This example will create curl handles for a list of URLs, add them to a multi
77
+
handle, and process them asynchronously.
61
78
</para>
62
79
<programlisting role="php">
63
80
<![CDATA[
64
81
<?php
65
-
// create both cURL resources
66
-
$ch1 = curl_init();
67
-
$ch2 = curl_init();
68
82
69
-
// set URL and other appropriate options
70
-
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
71
-
curl_setopt($ch1, CURLOPT_HEADER, 0);
72
-
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
73
-
curl_setopt($ch2, CURLOPT_HEADER, 0);
83
+
$urls = [
84
+
"https://www.php.net/",
85
+
"https://www.example.com/",
86
+
];
74
87
75
-
//create the multiple cURL handle
76
88
$mh = curl_multi_init();
89
+
$map = new WeakMap();
77
90
78
-
//add the two handles
79
-
curl_multi_add_handle($mh,$ch1);
80
-
curl_multi_add_handle($mh,$ch2);
91
+
foreach ($urls as $url) {
92
+
$ch = curl_init($url);
93
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
94
+
curl_multi_add_handle($mh, $ch);
95
+
$map[$ch] = $url;
96
+
}
81
97
82
-
$active = null;
83
-
//execute the handles
84
98
do {
85
-
$mrc = curl_multi_exec($mh, $active);
86
-
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
87
-
88
-
while ($active && $mrc == CURLM_OK) {
89
-
if (curl_multi_select($mh) != -1) {
90
-
do {
91
-
$mrc = curl_multi_exec($mh, $active);
92
-
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
99
+
$status = curl_multi_exec($mh, $unfinishedHandles);
100
+
if ($status !== CURLM_OK) {
101
+
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh)));
93
102
}
94
-
}
95
103
96
-
//close the handles
97
-
curl_multi_remove_handle($mh, $ch1);
98
-
curl_multi_remove_handle($mh, $ch2);
104
+
while (($info = curl_multi_info_read($mh)) !== false) {
105
+
if ($info['msg'] === CURLMSG_DONE) {
106
+
$handle = $info['handle'];
107
+
curl_multi_remove_handle($mh, $handle);
108
+
$url = $map[$handle];
109
+
110
+
if ($info['result'] === CURLE_OK) {
111
+
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
112
+
113
+
echo "Request to {$url} finished with HTTP status {$statusCode}:", PHP_EOL;
114
+
echo curl_multi_getcontent($handle);
115
+
echo PHP_EOL, PHP_EOL;
116
+
} else {
117
+
echo "Request to {$url} failed with error: ", PHP_EOL;
118
+
echo curl_strerror($info['result']);
119
+
echo PHP_EOL, PHP_EOL;
120
+
}
121
+
}
122
+
}
123
+
124
+
if ($unfinishedHandles) {
125
+
if (($updatedHandles = curl_multi_select($mh)) === -1) {
126
+
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh)));
127
+
}
128
+
}
129
+
} while ($unfinishedHandles);
130
+
99
131
curl_multi_close($mh);
100
132
101
133
?>
...
...
@@ -117,7 +149,6 @@ curl_multi_close($mh);
117
149
</refsect1>
118
150
119
151
</refentry>
120
-
121
152
<!-- Keep this comment at the end of the file
122
153
Local variables:
123
154
mode: sgml
124
155