<?php
/**
* Loads response headers for backward compatibility with retry logic.
*
* This function retrieves specific response headers (e.g., 'Retry-After', 'Cache-Control')
* and parses them to provide information useful for retry attempts. It offers
* backward compatibility by handling cases where certain headers might be missing.
*
* @param Psr7\ClientInterface|null $client The HTTP client. Defaults to null (using default client).
* @return array An associative array of response headers. Returns an empty array on failure.
*/
function getRetryHeaders(Psr7\ClientInterface|null $client = null): array
{
$headers = [];
if ($client === null) {
// Use default client if not provided.
$client = $this->getClient(); // Assuming getClient() is defined elsewhere and returns a Psr7\ClientInterface
}
try {
$response = $client->get('/'); // Replace '/' with the actual URL.
$headers = $response->getHeaders();
} catch (\Exception $e) {
// Handle exceptions during request. Return empty array on error.
error_log("Error getting response headers: " . $e->getMessage());
return [];
}
// Extract Retry-After header.
$retryAfter = $headers['Retry-After'] ?? null;
if ($retryAfter !== null && is_numeric($retryAfter)) {
$headers['retry_after'] = (int)$retryAfter; // Cast to integer
}
// Extract Cache-Control header.
$cacheControl = $headers['Cache-Control'] ?? '';
if (!empty($cacheControl)) {
//Further parsing of Cache-Control might be needed, but example is just to get the value.
$headers['cache_control'] = $cacheControl;
}
// Extract other relevant headers (e.g., Max-Retry) - Add more headers as needed.
$maxRetry = $headers['Max-Retry'] ?? null;
if ($maxRetry !== null && is_numeric($maxRetry)) {
$headers['max_retry'] = (int)$maxRetry;
}
return $headers;
}
/**
* Placeholder for getting the client. Must be defined elsewhere.
* @return Psr7\ClientInterface
*/
function getClient(): Psr7\ClientInterface
{
// Replace with your actual client retrieval logic.
// This is just a placeholder.
return new \GuzzleHttp\Client();
}
?>
Add your comment