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
.
<?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.
<?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.
<?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.
<?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.
<?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
- Timing: Clear headers immediately after processing them
- Middleware: Integrate clearing in request processing pipelines
- Testing: Always clear headers between test cases
- Error Handling: Clear headers in exception handlers
- Memory: Helps prevent memory leaks in long-running processes
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.