1. <?php
  2. /**
  3. * Flushes output for API endpoints for diagnostics.
  4. *
  5. * This function can be called to force output to be sent immediately,
  6. * useful for debugging API requests and responses.
  7. */
  8. function flushOutput() {
  9. ob_flush(); // Flush output buffer
  10. fflush(FILE, FIFO); // Flush file buffer
  11. }
  12. /**
  13. * Example usage within an API endpoint. Replace with your actual endpoint logic.
  14. *
  15. * @param string $requestMethod HTTP request method (e.g., 'GET', 'POST')
  16. * @param string $endpointPath The API endpoint path.
  17. */
  18. function apiEndpointHandler($requestMethod, $endpointPath) {
  19. //Simulate API logic
  20. if ($endpointPath === '/data') {
  21. $data = ['message' => 'This is some data'];
  22. header('Content-Type: application/json');
  23. echo json_encode($data);
  24. flushOutput(); // Flush output after sending the response
  25. } elseif ($endpointPath === '/error') {
  26. http_response_code(500); // Set an error code
  27. echo "Internal Server Error";
  28. flushOutput();
  29. }
  30. else {
  31. http_response_code(404);
  32. echo "Not Found";
  33. flushOutput();
  34. }
  35. }
  36. ?>

Add your comment