网站建设资讯

NEWS

网站建设资讯

php生成sitemapxml文件(网站地图)

最近帮朋友优化一个网站,想生成xml格式的sitemap然后提交给搜索引擎,利用php的simpleXML类就很容易实现了。贴一下代码块:

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都网站设计、印台网络推广、微信小程序定制开发、印台网络营销、印台企业策划、印台品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供印台建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

//sitemap_data.php 包含了网站所有链接的信息,直接贴出输出的数据,源码就不贴了
array(22) {
  [0]=>
  array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/index.php"
  }
  [1]=>
  array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/about.php"
  }
  [2]=>
  array(1) {
    ["loc"]=>
    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=1"
  }
  [3]=>
  array(1) {
    ["loc"]=>
    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=2"
  }
  [4]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=1"
  }
  [5]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=2"
  }
  [6]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=3"
  }
  [7]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=4"
  }
  [8]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=5"
  }
  [9]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=6"
  }
  [10]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=7"
  }
  [11]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=8"
  }
  [12]=>
  array(1) {
    ["loc"]=>
    string(43) "http://www.ibxg.com.cn/product.php?cat_id=9"
  }
  [13]=>
  array(1) {
    ["loc"]=>
    string(32) "http://www.ibxg.com.cn/order.php"
  }
  [14]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=1"
  }
  [15]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=2"
  }
  [16]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=3"
  }
  [17]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=4"
  }
  [18]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=5"
  }
  [19]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=6"
  }
  [20]=>
  array(1) {
    ["loc"]=>
    string(47) "http://www.ibxg.com.cn/project.php?project_id=7"
  }
  [21]=>
  array(1) {
    ["loc"]=>
    string(34) "http://www.ibxg.com.cn/contact.php"
  }
}

sitemap_xml.php文件




XML;

//$xml = simplexml_load_string($xml_wrapper);
$xml = new SimpleXMLElement($xml_wrapper);

foreach ($sitemap as $data) {
    $item = $xml->addChild('url'); //使用addChild添加节点
    if (is_array($data)) {
        foreach ($data as $key => $row) {
            $node = $item->addChild($key, $row);

            if (isset($attribute_array[$key]) && is_array($attribute_array[$key])) {
                foreach ($attribute_array[$key] as $akey => $aval) {//设置属性值,我这里为空
                    $node->addAttribute($akey, $aval);
                }
            }
        }
    }
}
echo $xml->asXML(); //用asXML方法输出xml,默认只构造不输出。
?>

另外网上也找到其他方法比如DOMDocument来构造xml,但通过比较使用simpleXML类是最省代码,实现起来也很简单。

其他方法可以参考这文章

http://www.phppan.com/2009/10/use-php-create-xml-file/


标题名称:php生成sitemapxml文件(网站地图)
新闻来源:http://cdweb.net/article/jpceic.html