<?php
/**
* API Payload Metrics Collector for Sandbox
*
* Collects metrics (size, headers, content type, errors) of API payloads
* for sandbox usage with simple error messages.
*/
class ApiMetricsCollector {
/**
* @param string $url The API endpoint URL.
* @param array $headers Optional headers to send with the request.
* @param array $data Optional data to send with the request (for POST/PUT).
* @return array An associative array containing metrics and errors.
*/
public function collectMetrics(string $url, array $headers = [], array $data = []): array {
$metrics = [];
$errors = [];
try {
// Simulate API request (replace with actual API client)
$response = $this->simulateApiRequest($url, $headers, $data);
if ($response === false) {
$errors[] = "API request failed: " . $this->getApiError();
return [
'size' => null,
'headers' => null,
'content_type' => null,
'errors' => $errors,
];
}
$metrics['size'] = strlen($response); // Payload size in bytes
$metrics['headers'] = $this->parseHeaders($response);
$metrics['content_type'] = $this->getContentType($response);
} catch (Exception $e) {
$errors[] = "An unexpected error occurred: " . $e->getMessage();
}
return [
'size' => $metrics['size'],
'headers' => $metrics['headers'],
'content_type' => $metrics['content_type'],
'errors' => $errors,
];
}
/**
* Simulates an API request. Replace with a real API client (e.g., cURL, Guzzle).
* @param string $url
* @param array $headers
* @param array $data
* @return string|false
*/
private function simulateApiRequest(string $url, array $headers = [], array $data = []): string|false {
// Simplified simulation - replace with real API call
if ($url === 'https://example.com/api/data') {
return "This is some sample data.";
} elseif ($url === 'https://example.com/api/error') {
return false; // Simulate an error
} else {
return "Simulated response for: " . $url;
}
}
/**
* Parses headers from the API response.
* @param string $response
* @return array
*/
private function parseHeaders(string $response): array {
$headers = [];
$lines = explode("\n", $response);
foreach ($lines as $line) {
if (strpos($line, ':') !== false) {
list($key, $value) = explode(': ', $line, 2); // Split into key and value
$headers[$key] = trim($value);
}
}
return $headers;
}
/**
* Determines the content type of the response.
* @param string $response
* @return string|null
*/
private function getContentType(string $response): ?string {
$lines = explode("\n", $response);
foreach ($lines as $line) {
if (strpos($line, 'Content-Type:') !== false) {
return trim(substr($line, strpos($line, ':') + 2));
}
}
return null;
}
/**
* Returns a generic error message.
* @return string
*/
private function getApiError(): string {
return "API returned an error.";
}
}
// Example Usage:
$collector = new ApiMetricsCollector();
$metrics = $collector->collectMetrics('https://example.com/api/data');
echo "Metrics:\n";
print_r($metrics);
$metrics = $collector->collectMetrics('https://example.com/api/error');
echo "\nMetrics with error:\n";
print_r($metrics);
?>
Add your comment