1. <?php
  2. /**
  3. * Suppresses response header errors and enables verbose logging for routine automation.
  4. *
  5. * @param callable $callback A callable (function) to execute. Should return a response object.
  6. * @return Response|null The response object, or null if an error occurred.
  7. */
  8. function runWithVerboseLogging(callable $callback): ?Response
  9. {
  10. try {
  11. // Suppress headers errors
  12. error_reporting(E_ALL & ~E_HEADER);
  13. ini_set('display_errors', 0); // Prevent errors from appearing in output
  14. $response = $callback();
  15. // Enable verbose logging
  16. $logMessage = "Response Headers: ";
  17. if (isset($response->headers)) {
  18. foreach ($response->headers as $header => $value) {
  19. $logMessage .= "$header: $value\n";
  20. }
  21. } else {
  22. $logMessage .= "No headers found.\n";
  23. }
  24. error_log($logMessage); // Log the response headers
  25. return $response;
  26. } catch (Exception $e) {
  27. error_log("Error during response processing: " . $e->getMessage());
  28. return null;
  29. }
  30. }
  31. /**
  32. * Example usage (replace with your actual HTTP request logic)
  33. */
  34. class Response {
  35. public $headers = [];
  36. public function __construct() {
  37. $this->headers = [];
  38. }
  39. }
  40. //Simulated HTTP request function
  41. function simulateHttpRequest(): Response {
  42. $response = new Response();
  43. $response->headers['Content-Type'] = 'application/json';
  44. $response->headers['X-Custom-Header'] = 'someValue';
  45. return $response;
  46. }
  47. // Example call.
  48. $response = runWithVerboseLogging(function () {
  49. return simulateHttpRequest();
  50. });
  51. if ($response) {
  52. // Process the response
  53. echo "Response received.\n";
  54. } else {
  55. echo "Error occurred during processing.\n";
  56. }
  57. ?>

Add your comment