ZetCode

PHP http_clear_last_response_headers Function

last modified April 4, 2025

The PHP http_clear_last_response_headers function clears the stored HTTP response headers. It's useful for resetting header state between requests.

Basic Definition

http_clear_last_response_headers removes all stored HTTP response headers from the last request. This affects functions that read these headers.

Syntax: http_clear_last_response_headers(): void. No parameters or return value. Part of the PECL HTTP extension. Requires PHP 5.1.0 or later.

Clearing After Header Retrieval

This example shows clearing headers after retrieving them with http_get_last_response_headers.

clear_after_retrieval.php
<?php

declare(strict_types=1);

// Make an HTTP request first
file_get_contents('https://example.com');

// Get and display headers
$headers = http_get_last_response_headers();
print_r($headers);

// Clear the stored headers
http_clear_last_response_headers();

// Verify headers are cleared
$headers = http_get_last_response_headers();
var_dump($headers); // NULL

This demonstrates the complete cycle of getting and clearing response headers. After clearing, subsequent calls to get headers will return NULL.

Resetting Between Multiple Requests

This example shows clearing headers between multiple HTTP requests to prevent mixing of response headers.

reset_between_requests.php
<?php

declare(strict_types=1);

// First request
file_get_contents('https://api.example.com/v1/users');
$headers1 = http_get_last_response_headers();

// Clear before next request
http_clear_last_response_headers();

// Second request
file_get_contents('https://api.example.com/v1/products');
$headers2 = http_get_last_response_headers();

echo "First request headers:\n";
print_r($headers1);

echo "\nSecond request headers:\n";
print_r($headers2);

Clearing headers between requests ensures each request's headers are isolated. This prevents accidental mixing of headers from different API calls.

Error Handling Scenario

This example demonstrates clearing headers when an error occurs during processing.

error_handling.php
<?php

declare(strict_types=1);

try {
    $response = file_get_contents('https://api.example.com/data');
    
    if ($response === false) {
        throw new Exception("Request failed");
    }
    
    // Process response
    $headers = http_get_last_response_headers();
    processHeaders($headers);
    
} catch (Exception $e) {
    // Clear any stored headers on error
    http_clear_last_response_headers();
    error_log("Error: " . $e->getMessage());
}

Clearing headers in error scenarios ensures no stale headers remain for future requests. This maintains clean state even when exceptions occur.

Testing Environment Setup

This example shows clearing headers in a testing environment to ensure isolated test cases.

testing_environment.php
<?php

declare(strict_types=1);

class ApiTest extends PHPUnit\Framework\TestCase {
    
    protected function tearDown(): void {
        // Clear headers after each test
        http_clear_last_response_headers();
    }
    
    public function testApiResponse() {
        file_get_contents('https://api.example.com/test');
        $headers = http_get_last_response_headers();
        
        $this->assertArrayHasKey('Content-Type', $headers);
    }
}

Using tearDown to clear headers ensures each test starts with a clean state. This prevents test interference from previous header data.

Middleware Implementation

This example demonstrates using the function in a middleware component to clean headers between requests.

middleware.php
<?php

declare(strict_types=1);

class HeaderCleanerMiddleware {
    
    public function __invoke($request, $handler) {
        $response = $handler->handle($request);
        
        // Clear stored headers after processing
        http_clear_last_response_headers();
        
        return $response;
    }
}

// Usage in a PSR-15 compatible framework
$app->add(new HeaderCleanerMiddleware());

Middleware is an ideal place to clear headers as it runs after each request. This pattern ensures headers don't leak between different HTTP requests.

Best Practices

Source

PHP http_clear_last_response_headers Documentation

This tutorial covered the PHP http_clear_last_response_headers function with practical examples for managing HTTP response headers.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Network Functions.