php生成站点地图sitemap

$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

感谢阅读这篇文章,如果你喜欢,或者遇到了问题,可以关注我的公众号