用PHP生成站点地图sitemap
它会自动抓取该页面上的所有链接地址,形成一个sitemap的xml格式文件。
不多说,直接上代码。把链接改成自己的博客主页。
<?php
function fetch_links_from_url($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$links = [];
$anchor_tags = $dom->getElementsByTagName('a');
foreach ($anchor_tags as $tag) {
$href = $tag->getAttribute('href');
// Filter out non-URL and non-HTTP links
if ($href && preg_match('/^(https?|\/\/)/', $href)) {
$links[] = $href;
}
}
return $links;
}
// Enter the URL of the webpage you want to scrape links from
$target_url = 'https://www.663333.xyz'; // Replace with your target URL
$links = fetch_links_from_url($target_url);
// Output the links in sitemap format
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($links as $link) { ?>
<url>
<loc><?= htmlspecialchars($link) ?></loc>
</url>
<?php } ?>
</urlset>本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
小菜鸡
评论已关闭