将数组传进去
生成xml文件就可以
送你个 改改
<?php
class xml {
private $data
private $encoding='utf-8' //XML文件编码
private $root='yeedao'
/*---------------------------------------
*实例化时传递参数
*$dataarray执行编译的数据
*---------------------------------------*/
public function __construct($data=''){
$this->data=$data
if( $this->data=='' ){
exit('请传递参数')
}
}
public function __toString(){
return $this->get_xml()
}
/*----------------------------------------
*生成XML
*----------------------------------------*/
public function get_xml() {
$xml = "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>"
$xml.= "<{$this->root}>"
$xml.= $this->make_xmk($this->data)
$xml.= "</{$this->root}>"
return $xml
}
/*-------------------------------------
*将数组或对象转换成XML-DOM格式
*-------------------------------------*/
protected function make_xmk($data) {
$xml = ''
foreach ($data as $key =>$value) {
//如果$key是一个数字,前面加item
(is_numeric($key)) ? $key='item'.$key : $key
$xml.="<{$key}>"
//如果遍历的$value还是一个array,回调
$xml.= ( is_array($value) || is_object($value)) ? $this->make_xmk($value) : $value
$xml.="</{$key}>"
}
return $xml
}
/*-----------------------------------------
*创建XML文件,成功后返回文件路径
*$path文件路径
*$filename文件名
*-----------------------------------------*/
public function touch_xml($path='',$filename=''){
$path=$_SERVER['DOCUMENT_ROOT'].$path
if($path{strlen($path)-1} != '/')$path.='/'
if(!is_dir($path))return '不是一个目录'
$filename=$path.$filename
touch ($filename)
chmod($filename,0777)
if(is_writeable($filename)){
if($handle=fopen($filename,'w+')){
if(fwrite($handle,$this->get_xml()) === false){
return '文件'.$filename.'写入失败'
}else{
return $filename //返回路径
}
}else{
return '不能打开文件'.$filename
}
}else{
return '文件'.$filename.'不可写'
}
}
}
?>
/***/
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条)