<?php
/**
* Collects metadata metrics with graceful failure handling.
*
* @param string $url The URL to analyze.
* @return array|null An array of metadata metrics, or null on failure.
*/
function collectMetadata(string $url): ?array
{
try {
// Initialize an empty array to store metadata metrics.
$metadata = [];
// Use cURL to fetch the URL.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the result as a string.
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers.
curl_setopt($ch, CURLOPT_NOBODY, true); // Only fetch headers.
$response = curl_exec($ch);
if ($response === false) {
error_log("cURL error: " . curl_error($ch)); // Log cURL errors.
curl_close($ch);
return null; // Return null on cURL failure.
}
curl_close($ch);
// Check if the request was successful (HTTP status code 200).
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
error_log("HTTP error: " . curl_getinfo($ch, CURLINFO_HTTP_CODE)); // Log HTTP errors.
return null; // Return null on HTTP error.
}
// Decode the headers.
$headers = get_headers($url);
if ($headers === false) {
error_log("Failed to get headers for " . $url);
return null;
}
// Extract metadata from the headers.
if (count($headers) > 0) {
$metadata['content_type'] = $headers[0][0]; // Content type.
$metadata['server'] = $headers[0][1]; // Server.
$metadata['date'] = $headers[0][2]; // Date.
} else {
error_log("No headers found for " . $url);
return null;
}
// Example: Extract the size
$metadata['size'] = curl_getinfo($ch, CURLINFO_CONTENT_SIZE);
return $metadata;
} catch (Exception $e) {
error_log("Exception: " . $e->getMessage()); // Log exceptions.
return null; // Return null on exception.
}
}
// Example usage:
$url = 'https://www.example.com';
$metrics = collectMetadata($url);
if ($metrics !== null) {
print_r($metrics); // Output the collected metrics.
} else {
echo "Failed to collect metadata for " . $url . "\n"; // Indicate failure.
}
?>
Add your comment