*/
class sitemap{
private $sitemapFile = array()
private $oldxml = null
private $newxml = null
public $error = null
public function __construct($sitemapFile) {
$this->sitemapFile = $sitemapFile
if(is_file($this->sitemapFile)) {
$data = file_get_contents($this->sitemapFile)
if($data) {
$this->oldxml = new SimpleXMLElement($data)
}else{
$this->error = '读取站点地图文件失败'
}
}else{
$this->oldxml = $this->createEmptySitemap()
}
$this->newxml = $this->createEmptySitemap()
}
public function createEmptySitemap() {
$str = '<?xml version="1.0" encoding="UTF-8"?>'
$str .= '<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"> </urlset>'
return new SimpleXMLElement($str)
}
public function addChilds($urlArr) {
$urlArr = (array) $urlArr
foreach($urlArr as $url) {
$priority = 0.5
$lastmod = date('Y-m-d')
$changefreq = 'weekly'
if(stripos($url,'.html')) {
$priority = 1
$changefreq = 'monthly'
}
if($oldXmlUrl = $this->findOldXmlUrl($url)) {
$priority = $oldXmlUrl->priority
$lastmod = $oldXmlUrl->lastmod
$changefreq = $oldXmlUrl->changefreq
}
$rating = $this->newxml->addChild('url')
$rating->addChild('loc',$url)
$rating->addChild('priority',$priority)
$rating->addChild('lastmod',$lastmod)
$rating->addChild('changefreq',$changefreq)
}
}
public function findOldXmlUrl($url) {
$oldXmlUrl = ''
foreach($this->oldxml->url as $key=>$xmlUrl) {
if($xmlUrl->loc == $url) {
$oldXmlUrl = $xmlUrl
unset($this->oldxml->url[$key])
break
}
}
return $oldXmlUrl
}
public function save() {
$data = $this->newxml->asXML()
if(file_put_contents($this->sitemapFile,$data) === false) {
$this->error = '写入站点地图数据失败'
return false
}
return true
}
}
上面这个是我个人博客生成站点地图用的类。
客户端调用代码如下:
$sitemapFile = 'Sitemap.xml'$sitemap = new sitemap($sitemapFile)
if($sitemap->error) {
die($sitemap->error)
}
$newUrl = [
'http://www.kiscms.com/content/28.html'
]
$sitemap->addChilds()
if(!$sitemap->save()) {
die($sitemap->error)
}
关键的问题是,你如何得到整站的url呢?
我个人博客的解决方法是写了个蜘蛛程序爬出来的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)