Current Location: Home> Latest Articles> Detailed Guide on How to Properly Execute Requests and Handle Responses After Adding Handles with curl_multi_add_handle

Detailed Guide on How to Properly Execute Requests and Handle Responses After Adding Handles with curl_multi_add_handle

gitbox 2025-08-23
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This section is unrelated to the article content, it can be initialization or other setup</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Initializing PHP environment...\n"</span></span><span>;
</span><span><span class="hljs-variable">$version</span></span><span> = </span><span><span class="hljs-title function_ invoke__">phpversion</span></span><span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current PHP version: <span class="hljs-subst">$version</span>\n";</span></span>
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
// Article content starts</p>
<p>echo "<h1>Detailed Guide on How to Properly Execute Requests and Handle Responses After Adding Handles with curl_multi_add_handle</h1>";</p>
<p>echo <span><span class="hljs-string">"<p>In PHP, using <code>cURL
;

echo "

3. Add Individual Handles to the Multi-Handle Manager

";

echo "

Use curl_multi_add_handle() to add each individual handle to the manager:

";

echo

curl_multi_add_handle($mh, $ch1);<br>
curl_multi_add_handle($mh, $ch2);
;

echo "

4. Execute Concurrent Requests

";

echo "

Once added, loop through curl_multi_exec() until all requests are complete:

";

echo

$running = null;<br>
do {<br>
$status = curl_multi_exec($mh, $running);<br>
// Optional: wait for file descriptor changes to reduce CPU usage<br>
curl_multi_select($mh);<br>
} while ($running > 0);
;

echo "

5. Retrieve the Responses

";

echo "

After completion, use curl_multi_getcontent() to get the response from each handle:

";

echo

$response1 = curl_multi_getcontent($ch1);<br>
$response2 = curl_multi_getcontent($ch2);</p>
<p>echo "Response 1: " . $response1 . "\n";<br>
echo "Response 2: " . $response2 . "\n";
;

echo "

6. Remove and Close Handles

";

echo "

Finally, clean up resources by removing handles from the manager and closing both individual and multi-handles:

";

echo

curl_multi_remove_handle($mh, $ch1);<br>
curl_multi_remove_handle($mh, $ch2);</p>
<p>curl_close($ch1);<br>
curl_close($ch2);<br>
curl_multi_close($mh);
;

echo "

Conclusion

";

echo "

The key steps to correctly execute requests and obtain results after using curl_multi_add_handle are:

";

echo "


  1. Create individual cURL handles and set return options.

  2. Initialize the multi-handle manager.

  3. Add handles to the manager.

  4. Loop through curl_multi_exec until complete.

  5. Use curl_multi_getcontent to retrieve responses.

  6. Remove and close handles to release resources.

";

echo "

By following these steps, you can efficiently perform concurrent HTTP requests in PHP while ensuring that the returned data is properly handled.

";

?>