Current Location: Home> Latest Articles> ob_get_contents() captures and displays the results of an asynchronous request

ob_get_contents() captures and displays the results of an asynchronous request

gitbox 2025-05-28

What is ob_get_contents()?

ob_get_contents() is used to get the contents in the current PHP output buffer. Combined with ob_start() , it allows us to save the output content first, rather than send it directly to the browser, so that we can process or store the output secondaryly.

 ob_start();  // Turn on the output buffer
echo "Hello, world!";
$content = ob_get_contents();  // Get the buffer content
ob_end_clean();  // Close the buffer and clear the contents
echo "Captured content: " . $content;

The above code will capture the string output by echo , store it in $content , and then output it.


Why use ob_get_contents() to capture the results of asynchronous requests?

Asynchronous requests generally refer to the client sending a request to the server through JavaScript (such as AJAX) and obtaining the return result without refreshing the page. When PHP processes these requests on the server side, it sometimes needs to capture the output content and then return it to the front end after processing.

Through ob_get_contents() , the execution results of asynchronous requests (such as the content rendered by a template) can be captured in the background, and then returned to the front-end as a response, enhancing flexibility.


PHP Asynchronous Request Sample Teaching

Suppose you send a request to async_handler.php through AJAX, PHP needs to return a dynamically generated content. We use ob_get_contents() to capture the output and return it to the caller.

async_handler.php

 <?php
// Turn on output buffering
ob_start();

// Simulate asynchronous processing logic
echo "<div>This is what is returned asynchronously,Current time:" . date('H:i:s') . "</div>";

// Capture buffer content
$content = ob_get_contents();

// Close and clean the buffer
ob_end_clean();

// Output the captured content as a response
echo $content;
?>

Front-end AJAX call example (based on jQuery)

 $.ajax({
    url: 'http://gitbox.net/async_handler.php',
    method: 'GET',
    success: function(response) {
        // Insert the returned content into the page
        $('#result').html(response);
    },
    error: function() {
        alert('Request failed');
    }
});

Combined with curl to implement asynchronous request capture (server-side call)

Sometimes we want to initiate asynchronous requests within PHP and get the results, which can also be implemented using ob_get_contents() combined with curl .

 <?php
function getAsyncContent($url) {
    ob_start();

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result; // Output curl Return to content

    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}

$url = "http://gitbox.net/async_handler.php";
$response = getAsyncContent($url);
echo "Asynchronous request result on the server:" . $response;
?>

Summarize

  • ob_get_contents() allows you to capture the output buffer content of PHP scripts and flexibly process output data.

  • In asynchronous request processing, the results are captured using buffers to easily control and return dynamic content.

  • Whether it is a front-end AJAX request or a PHP server initiates asynchronous calls, ob_get_contents() can help you obtain and operate output, enhancing the scalability and maintainability of your application.

With this technology mastered, you will be more comfortable when dealing with complex asynchronous logic.