loadHTML($response);
$xpath = new DOMXPath($dom);
// Extract links
$results = [];
foreach ($xpath->query("//a[contains(@href, '/url?q=')]") as $node) {
$href = $node->getAttribute("href");
$parsedUrl = explode("&", explode("/url?q=", $href)[1])[0];
$results[] = $parsedUrl;
}
return array_slice($results, 0, 10);
}
function fetchLastModified($url) {
$headers = get_headers($url, 1);
return isset($headers['Last-Modified']) ? $headers['Last-Modified'] : null;
}
function fetchCreationDate($url) {
// Fetch HTML content
$html = file_get_contents($url);
if (!$html) return null;
$dom = new DOMDocument();
@$dom->loadHTML($html);
// Check meta tags for publication dates
$metaTags = $dom->getElementsByTagName('meta');
foreach ($metaTags as $meta) {
$name = $meta->getAttribute('name');
$property = $meta->getAttribute('property');
$content = $meta->getAttribute('content');
if (in_array($name, ['creation_date', 'date', 'publish_date', 'datePublished']) ||
in_array($property, ['article:published_time', 'og:published_time'])) {
return $content;
}
}
return null;
}
function parseDate($dateStr) {
$date = DateTime::createFromFormat(DateTime::RFC2822, $dateStr) ?: DateTime::createFromFormat('Y-m-d\TH:i:s', $dateStr) ?: DateTime::createFromFormat('Y-m-d', $dateStr);
return $date ?: null;
}
function calculatePageAges($urls) {
$ages = [];
$now = new DateTime();
foreach ($urls as $url) {
// Check Last-Modified header
$lastModified = fetchLastModified($url);
if ($lastModified) {
$modifiedDate = parseDate($lastModified);
if ($modifiedDate) {
$ages[] = $now->diff($modifiedDate)->days;
continue;
}
}
// Check creation date in HTML
$creationDate = fetchCreationDate($url);
if ($creationDate) {
$parsedDate = parseDate($creationDate);
if ($parsedDate) {
$ages[] = $now->diff($parsedDate)->days;
}
}
}
return $ages;
}
// Main execution
$query = "example search query"; // Replace with user input
$urls = fetchGoogleResults($query);
$pageAges = calculatePageAges($urls);
if (!empty($pageAges)) {
$averageAge = array_sum($pageAges) / count($pageAges);
echo "The average age of the pages is " . round($averageAge, 2) . " days.\n";
} else {
echo "No valid dates found for the pages.\n";
}
?>