php生成站点地图sitemap

编辑于 2021-05-15 15:50:55 阅读 1399
$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

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验