php生成站点地图sitemap

2022-05-25 20:19:50 浏览数 (1)

代码语言:javascript复制
$data=[
    ['loc'=>'https://www.cuiwei.net/', 'lastmod'=>'2009-01-01'],//首页
];

$xml=createXML($data);
file_put_contents('sitemap.xml', $xml);

function createXML($data){
    $string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
</urlset>
XML;
    $xml = simplexml_load_string($string);
    foreach ($data as $item) {
        $url = $xml->addChild('url');
        if (is_array($item)) {
            foreach ($item as $key => $row) {
                $node = $url->addChild($key, $row);
            }
        }
    }
    return $xml->asXML();
}

参考标准

https://www.sitemaps.org/protocol.html

0 人点赞