Here is a PHP snippet that fetches the contents of a XML sitemap on a remote website. Then using a foreach we loop through the list of URLs to fetch the Page Title (aka meta title) and the meta description for each of the URLs in XML Sitemap.
Additionally, you can also display the elements of the XML sitemap - URL, Lastmod date, Change Frequency and Priority.
Please note* this script is for testing purposes only. The script may take some time to fully execute against large XML sitemap files.
Additionally, you can also display the elements of the XML sitemap - URL, Lastmod date, Change Frequency and Priority.
PHP:
<?php
// get sitemap url
$sitemap = 'https://example.com/sitemap.xml';
// get sitemap contents
$content = file_get_contents($sitemap);
// parse the sitemap contents
$xml = simplexml_load_string($content);
// retrieve elements from sitemap contents
foreach ($xml->url as $urlElement) {
// get elements
$url = $urlElement->loc;
$lastmod = $urlElement->lastmod;
$changefreq = $urlElement->changefreq;
$priority = $urlElement->priority;
// print out the properties
// echo 'url: '. $url . '<br>';
// echo 'lastmod: '. $lastmod . '<br>';
// echo 'changefreq: '. $changefreq . '<br>';
// echo 'priority: '. $priority . '<br>';
// echo '---<br>'; // seperator
/* lets begin the foreach output */
foreach ($url as $page) {
$html = file_get_contents($page);
sleep(1);
//echo $source; // echos list of URLs
// echo $html; // echos the content - uncomment with caution
// start url parsing:
$doc = new DOMDocument();
@$doc->loadHTML($html);
// fetch and display page title and meta description:
$items = $doc->getElementsByTagName('title');
$title = $items->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
}
echo "<strong>URL:</strong> ". $url . '<br>';
echo "<strong>Title</strong> : $title". '<br/>';
echo "<strong>Description</strong> : $description". '<br/>';
echo '<br>---<br>';
}
}
?>
Please note* this script is for testing purposes only. The script may take some time to fully execute against large XML sitemap files.
Last edited: