使用PHP动态创建Sitemap

时间:2019-01-29 09:00:37

标签: php xml http-headers generator sitemap

我正在尝试通过php scritp生成有效的站点地图。通用方案在.htaccess中很简单,我将所有^(。+)index_sitemal.xml请求转发到我的index_sitemap.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>';
?>

在浏览器中显示良好。

图像链接-(对不起,信誉低)https://i.ibb.co/4ZmLJ1D/Screenshot-at-Jan-29-10-47-01.png

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

图像链接-(对不起,信誉低)https://i.ibb.co/Ws11cBj/Screenshot-at-Jan-29-10-55-28.png

是否可以使用php显示动态站点地图?

1 个答案:

答案 0 :(得分:-1)

以前我也遇到过类似的问题,但是通过运行一个不同的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';
}

?>