<?php
/**
* Formats and logs response headers for an experiment.
*
* @param array $headers The response headers array.
* @param string $method The HTTP method (e.g., 'GET', 'POST').
* @param string $url The requested URL.
* @param string $response_body The response body.
* @param string $experiment_id The ID of the experiment.
*/
function formatAndLogHeaders(array $headers, string $method, string $url, string $response_body, string $experiment_id): void
{
$log_message = "--- Experiment Log ---\n";
$log_message .= "Method: " . $method . "\n";
$log_message .= "URL: " . $url . "\n";
$log_message .= "Response Body (first 100 chars): " . substr($response_body, 0, 100) . "...\n";
$log_message .= "\nResponse Headers:\n";
foreach ($headers as $header => $value) {
$log_message .= " " . $header . ": " . $value . "\n";
}
$log_message .= "\n--- End of Log ---\n";
// Error logging (replace with your preferred logging mechanism)
error_log($log_message, 3, '/path/to/your/error.log'); // Log to file
// Optionally, you can also log to a database or other system.
}
//Example Usage (replace with your actual values)
/*
$headers = [
'Content-Type' => 'application/json',
'X-Custom-Header' => 'ExperimentData',
'Server' => 'MyServer/1.0'
];
$method = 'POST';
$url = '/api/endpoint';
$response_body = '{"status": "success", "data": []}';
$experiment_id = 'exp123';
formatAndLogHeaders($headers, $method, $url, $response_body, $experiment_id);
*/
?>
Add your comment