<?php
/**
* Flushes output for API endpoints for diagnostics.
*
* This function can be called to force output to be sent immediately,
* useful for debugging API requests and responses.
*/
function flushOutput() {
ob_flush(); // Flush output buffer
fflush(FILE, FIFO); // Flush file buffer
}
/**
* Example usage within an API endpoint. Replace with your actual endpoint logic.
*
* @param string $requestMethod HTTP request method (e.g., 'GET', 'POST')
* @param string $endpointPath The API endpoint path.
*/
function apiEndpointHandler($requestMethod, $endpointPath) {
//Simulate API logic
if ($endpointPath === '/data') {
$data = ['message' => 'This is some data'];
header('Content-Type: application/json');
echo json_encode($data);
flushOutput(); // Flush output after sending the response
} elseif ($endpointPath === '/error') {
http_response_code(500); // Set an error code
echo "Internal Server Error";
flushOutput();
}
else {
http_response_code(404);
echo "Not Found";
flushOutput();
}
}
?>
Add your comment