Current Location: Home> Latest Articles> curl_multi_close How to handle redirected responses when closing a request

curl_multi_close How to handle redirected responses when closing a request

gitbox 2025-05-12

The curl_multi_* series functions are very common when using PHP's cURL extension for concurrent requests. curl_multi_close() is a function used to release resources and close the cURL handle collection. However, many developers may wonder when dealing with responses including redirects (such as 301, 302): And, what details should we pay attention to?

This article will teach you to use curl_multi_close() correctly from a principle and practical perspective to avoid losing important redirect responses when closing a request.

Understand the basic role of curl_multi_close

First of all, you need to understand that the main functions of curl_multi_close($multi_handle) are:

  • Close a multi handler

  • Release all associated resources at the same time

Important tips :

  • curl_multi_close() is only responsible for resource cleaning and will not automatically handle redirects .

  • The processing of redirects must be completed during the request execution phase (such as setting options before curl_multi_exec() ).

  • After closing, you can no longer get any request response data, including redirect information.

Therefore, before calling curl_multi_close() , you must make sure that all requests have been fully executed and that the required response has been read.

Correctly handle redirect responses

To get cURL to follow the redirect correctly and get the final response before closing, you should do this:

  1. Set cURL options to allow automatic follow-up redirects.

  2. Completely execute the request and retrieve all required data.

  3. Finally, curl_multi_close() is called to release the resource.

Sample code

 <?php
// Create a multi-handle processor
$multiHandle = curl_multi_init();

// Initialize a separate cURL Session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gitbox.net/redirect-example');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allow automatic follow-up redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // Follow at most5Redirection

// 将Session添加到多句柄处理器
curl_multi_add_handle($multiHandle, $ch);

// Execute a request
$running = null;
do {
    $status = curl_multi_exec($multiHandle, $running);
    if ($status > 0) {
        echo "cURL Error: " . curl_multi_strerror($status);
    }
    // Waiting for an active connection
    curl_multi_select($multiHandle);
} while ($running > 0);

// Get content and information before closing
$response = curl_multi_getcontent($ch);
$info = curl_getinfo($ch);

// Print response content
echo "Final URL: " . $info['url'] . PHP_EOL;
echo "HTTP Code: " . $info['http_code'] . PHP_EOL;
echo "Response Body: " . $response . PHP_EOL;

// Cleaning
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
curl_multi_close($multiHandle);
?>

Key interpretation

  • CURLOPT_FOLLOWLOCATION is set to true to automatically handle redirects.

  • After curl_multi_exec is fully executed, use curl_multi_getcontent() and curl_getinfo() to get the final response information.

  • Curl_multi_close() can only be called safely after fully fetching the data.

If you do not handle these steps correctly before curl_multi_close() , you will not be able to get the redirected final response data.

Common error examples

Here is a mistake-prone way to write it:

 <?php
$multiHandle = curl_multi_init();
$ch = curl_init('https://gitbox.net/redirect-example');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiHandle, $ch);

// mistake:Close it directly if the execution is not completed
curl_multi_close($multiHandle);

$response = curl_multi_getcontent($ch); // The content obtained at this time may be empty or undefined
?>

Problem analysis :
The multi handle is closed before the request has been completed, resulting in the subsequent inability to obtain the redirect response and data correctly.

summary

  • curl_multi_close() does not handle any redirects , it is only responsible for cleaning resources.

  • Redirection must be set by setting CURLOPT_FOLLOWLOCATION and the request processing is completed before closing.

  • Be sure to extract all required data before curl_multi_close() .

  • In the correct order (Execute → Get content → Remove handle → Close multi) to ensure that the request is complete and the data is not lost.

Mastering these details can make you easy to handle complex concurrent requests, especially when it comes to redirecting responses!