Skip to content

Instantly share code, notes, and snippets.

@JRMorris77
Created January 19, 2023 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRMorris77/a337f4d1acb4f1ee66826e3086043515 to your computer and use it in GitHub Desktop.
Save JRMorris77/a337f4d1acb4f1ee66826e3086043515 to your computer and use it in GitHub Desktop.
<?php
// URL of the sitemap
$sitemapUrl = "https://www.example.com/sitemap.xml";
// Load the sitemap XML into a SimpleXML object
$sitemapXml = simplexml_load_file($sitemapUrl);
// Iterate over each URL in the sitemap
foreach ($sitemapXml->url as $url) {
// Get the page URL
$pageUrl = $url->loc;
// Create a new DOM document
$doc = new DOMDocument();
// Load the website HTML into the DOM
@$doc->loadHTMLFile($pageUrl);
// Get all image elements
$imageTags = $doc->getElementsByTagName('img');
// Iterate over each image
foreach ($imageTags as $tag) {
// Get the image source URL
$imgUrl = $tag->getAttribute('src');
// Download the image
file_put_contents('images/' . basename($imgUrl), file_get_contents($imgUrl));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment