find('a') as $link){ $url = $link->href; // Check if the URL is a relative URL and add the base URL if necessary if(substr($url, 0, 1) == "/"){ $url = $base_url . $url; } // Check if the URL is already in the array if(!in_array($url, $urls)){ // Add the URL to the array $urls[] = $url; // If the URL is a page on the same domain, crawl it and add its URLs to the array if(strpos($url, $base_url) !== false){ $page_content = file_get_contents($url); $page_dom = str_get_html($page_content); foreach($page_dom->find('a') as $page_link){ $page_url = $page_link->href; // Check if the URL is a relative URL and add the base URL if necessary if(substr($page_url, 0, 1) == "/"){ $page_url = $base_url . $page_url; } // Check if the URL is already in the array if(!in_array($page_url, $urls)){ $urls[] = $page_url; } } } } } // Create an XML sitemap using SimpleXMLElement $sitemap = new SimpleXMLElement(''); $sitemap->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); // Loop through the URLs and add them to the sitemap foreach($urls as $url){ $url_elem = $sitemap->addChild('url'); $url_elem->addChild('loc', $url); $url_elem->addChild('lastmod', date('c', time())); $url_elem->addChild('changefreq', 'monthly'); $url_elem->addChild('priority', '0.5'); } // Output the sitemap to a file on the server $sitemap_filename = 'sitemap.xml'; $sitemap_path = $_SERVER['DOCUMENT_ROOT'] . '/test/' . $sitemap_filename; $sitemap->asXML($sitemap_path); echo "Sitemap generated successfully!"; ?>