使用 PHP 动态创建站点地图

Sitemap Dynamic Creation with PHP

我正在尝试使用 php 生成有效的站点地图。逻辑很简单。我将所有 ^(.+)index_sitemal.xml 请求转发到我在 .htaccess 中的 index_sitemap.php 文件。 PHP 脚本如下:

<?php 
header( "content-type: application/xml; charset=UTF-8" );
    echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
    $len = 10;   // to take
    $min = 50;  // minimum
    $max = 100;  // maximum
    $range = [];
    foreach (range(0, $len - 1) as $i) {
        while(in_array($num = mt_rand($min, $max), $range));
        $range[] = $num;
        echo '<sitemap><loc>http://'.$_SERVER['SERVER_NAME'].'/sitemap/'.$num.'.xml</loc></sitemap>'."\n";
    }
    echo '</sitemapindex>';
?>

在浏览器中显示良好。

Link 用于图像 -(抱歉声誉低)https://i.ibb.co/4ZmLJ1D/Screenshot-at-Jan-29-10-47-01.png

但是在尝试验证 xml 时出现类型错误。

Link 用于图像 -(抱歉声誉低)https://i.ibb.co/Ws11cBj/Screenshot-at-Jan-29-10-55-28.png

有什么方法可以使用 php 显示动态站点地图吗?

我之前遇到过类似的问题,但通过 运行 一个不同的 PHP 文件解决了它,以使用 CRON 作业更新站点地图。

<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';

$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/videos/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';

$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/contact/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';

$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/blog/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';

$sql = "SELECT * FROM categories";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $url = $row["url"];
    $xmlString .= '<url>';
    $xmlString .= '<loc>http://example.com/category/'.htmlentities($url).'/</loc>';
    $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
    $xmlString .= '<changefreq>daily</changefreq>';
    $xmlString .= '<priority>1.0</priority>';
    $xmlString .= '</url>';
}

$xmlString .= '</urlset>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);

$dom->save('../sitemap.xml');
?>

编辑

<?php

$host = 'http://www.google.com/ping?sitemap=https://www.example.com/sitemap.xml'; 
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
  echo 'success';
} else {
  echo 'try again';
}

?>