1. <?php
  2. /**
  3. * API Payload Metrics Collector for Sandbox
  4. *
  5. * Collects metrics (size, headers, content type, errors) of API payloads
  6. * for sandbox usage with simple error messages.
  7. */
  8. class ApiMetricsCollector {
  9. /**
  10. * @param string $url The API endpoint URL.
  11. * @param array $headers Optional headers to send with the request.
  12. * @param array $data Optional data to send with the request (for POST/PUT).
  13. * @return array An associative array containing metrics and errors.
  14. */
  15. public function collectMetrics(string $url, array $headers = [], array $data = []): array {
  16. $metrics = [];
  17. $errors = [];
  18. try {
  19. // Simulate API request (replace with actual API client)
  20. $response = $this->simulateApiRequest($url, $headers, $data);
  21. if ($response === false) {
  22. $errors[] = "API request failed: " . $this->getApiError();
  23. return [
  24. 'size' => null,
  25. 'headers' => null,
  26. 'content_type' => null,
  27. 'errors' => $errors,
  28. ];
  29. }
  30. $metrics['size'] = strlen($response); // Payload size in bytes
  31. $metrics['headers'] = $this->parseHeaders($response);
  32. $metrics['content_type'] = $this->getContentType($response);
  33. } catch (Exception $e) {
  34. $errors[] = "An unexpected error occurred: " . $e->getMessage();
  35. }
  36. return [
  37. 'size' => $metrics['size'],
  38. 'headers' => $metrics['headers'],
  39. 'content_type' => $metrics['content_type'],
  40. 'errors' => $errors,
  41. ];
  42. }
  43. /**
  44. * Simulates an API request. Replace with a real API client (e.g., cURL, Guzzle).
  45. * @param string $url
  46. * @param array $headers
  47. * @param array $data
  48. * @return string|false
  49. */
  50. private function simulateApiRequest(string $url, array $headers = [], array $data = []): string|false {
  51. // Simplified simulation - replace with real API call
  52. if ($url === 'https://example.com/api/data') {
  53. return "This is some sample data.";
  54. } elseif ($url === 'https://example.com/api/error') {
  55. return false; // Simulate an error
  56. } else {
  57. return "Simulated response for: " . $url;
  58. }
  59. }
  60. /**
  61. * Parses headers from the API response.
  62. * @param string $response
  63. * @return array
  64. */
  65. private function parseHeaders(string $response): array {
  66. $headers = [];
  67. $lines = explode("\n", $response);
  68. foreach ($lines as $line) {
  69. if (strpos($line, ':') !== false) {
  70. list($key, $value) = explode(': ', $line, 2); // Split into key and value
  71. $headers[$key] = trim($value);
  72. }
  73. }
  74. return $headers;
  75. }
  76. /**
  77. * Determines the content type of the response.
  78. * @param string $response
  79. * @return string|null
  80. */
  81. private function getContentType(string $response): ?string {
  82. $lines = explode("\n", $response);
  83. foreach ($lines as $line) {
  84. if (strpos($line, 'Content-Type:') !== false) {
  85. return trim(substr($line, strpos($line, ':') + 2));
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * Returns a generic error message.
  92. * @return string
  93. */
  94. private function getApiError(): string {
  95. return "API returned an error.";
  96. }
  97. }
  98. // Example Usage:
  99. $collector = new ApiMetricsCollector();
  100. $metrics = $collector->collectMetrics('https://example.com/api/data');
  101. echo "Metrics:\n";
  102. print_r($metrics);
  103. $metrics = $collector->collectMetrics('https://example.com/api/error');
  104. echo "\nMetrics with error:\n";
  105. print_r($metrics);
  106. ?>

Add your comment