Created
January 19, 2023 21:39
-
-
Save JRMorris77/a337f4d1acb4f1ee66826e3086043515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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