程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php生成百度sitemap站點地圖類函數實例

php生成百度sitemap站點地圖類函數實例

編輯:關於PHP編程

     問題概述:

    公司網站是問答百科的網站、seo工程師提出需求說根據網站的問題來生成xml文件。每個xml文件包含5000條setmap格式數據。現在線上網站大約有70w條問題,所以說基本生成140個xml文件。還有一個索引文件。比如文件的名稱以數字開頭的。索引文件包含的內容就是每個xml文件的路徑還有名稱。
    為什麼要每個文件存儲5000條數據呢,因為這是mysql的一個界限值、如果每次取多了以後可能會對線上用戶訪問造成影響,或者速度變慢。每個文件存儲5000條數據,但是mysql selsect的時候不能每次取5000條、現在寫的是每次取1000條。那這樣邏輯就有點復雜。

    實現方法:

    首先取出1000條數據(可以靈活些成活的,方便以後修改),然後循環生成xml格式文件。file_puts_contens寫入文件。然後再把生成的xml文件名稱、取出問題的最小id、取出問題的最大id、取出問題的條數寫出一個索引查詢的txt文件當中,格式大概是這個樣子的。
    0,3146886,3145887,1000
    發現最後面的條數是1000了嗎、第一次select取出1000條數據、然後寫入0.xml文件當中。把取出的xml文件名稱、最小id、最大id、條數寫入到索引查詢txt中。第一次寫入了1000條數據到0.xml、生成條數為1000。第二次查詢的時候select語句會成為。 where id > 取出的最大id(當前mysql為正序查詢、如果為倒序、改成小於) limit 1000 這樣的話又取出1000、然後修改索引查詢txt的最小id、最大id、生成條數加到2000。以此類推等生成條數到了5000的時候再另起一行寫入索引文件、類似這樣
    0,3146886,3145887,5000
    1,3148886,3147887,1000
    這樣寫的話就減輕了服務器的壓力。
    下面貼出實現代碼(風格有點亂):

    具體功能代碼如下:

     代碼如下: <?php
    /*
     * SiteMap接口類
     */
     
    class SitemapAction extends Action{
    private static $baseURL = ''; //URL地址
    private static $askMobileUrl = 'http://m.xxx.cn/ask/'; //問答移動版地址
    private static $askPcUrl = "http://www.xxx.cn/ask/";   //問答pc地址
    private static $askZonePcUrl = "http://www.xxx.cn/ask/jingxuan/"; //問答精選Pc鏈接
    private static $askZoneMobileUrl = "http://m.xxx.cn/ask/jx/"; //問答精選移動版鏈接
            //問答setmaps
    public function askSetMap(){
    header('Content-type:text/html;charset=utf-8');
    //獲取問題列表
    $maxid = 0;    //索引文件最大id
    $minid = 0;    //索引文件最小id
    $psize = 1000; //數據庫每次取數量
    $maxXml = 5000; //xml寫入記錄數量
    $where = array();
    //讀取索引文件
    $index = APP_PATH.'setmapxml/Index.txt';
    //關聯setmaps路徑
    $askXml = "../siteditu/ask/ask.xml";
    if(!file_exists($index)){
    $fp=fopen("$index", "w+");
    if ( !is_writable($index) ){
    die("文件:" .$index. "不可寫,請檢查!");
    }
    fclose($fp);
    }else{
    //index.txt文件說明 0:xml文件名稱(從1開始)、1:文件最大id、2:文件最小id、3:文件當前記錄數
    $fp = file($index);
    $string = $fp[count($fp)-1];//顯示最後一行
    $arr = explode(',', $string);
    }
    //索引文件數量是否小於$maxXml
    //如果為第一次運行
    if(!$arr[1]){
    $bs=1;
    $filename=0;
    }else{
    if($arr && $arr[3]<$maxXml){
    $filename = $arr[0];
    $psize = $maxXml-$arr[3]>$psize?$psize:($maxXml-$arr[3]);
    $bs = 0;
    }else{
    $filename = $arr[0]+1;
    $bs=1;
    }
    }
    $maxid = empty($arr[1])?0:$arr[1];
    $minid = empty($arr[2])?0:$arr[2];
    echo "文件名稱:".$filename.".xml"."<br/ >";
    echo "最大id:".$maxid."<br />";
    echo "最小id:".$minid."<br />";
    echo "xml寫入最大記錄:".$maxXml."<br />";
    echo "數據庫每次讀取數量:".$psize."<br />";
    $list = self::$questionObj->getQuestionSetMap($where,$maxid,$psize);
    if(count($list)<=0){
    echo 1;exit;
    }
    $record = $arr[3]+count($list); //索引文件寫入記錄數
    $indexArr = array('filename'=>$filename,'maxid'=>$maxid,'minid'=>$minid,'maxXml'=>$record);
    $start = '<?xml version="1.0" encoding="UTF-8" ?> '.chr(10);
    $start.="<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">".chr(10);
                                    $start.="</urlset>";
    foreach($list as $k=>$qinfo){
    if($k==0)
    $indexArr['minid']=$qinfo['id'];
    $qinfo['lastmod'] = substr($qinfo['lasttime'],0,10);
    $qinfo['mobielurl'] = self::$askMobileUrl.$qinfo['id'].'.html'; //移動版鏈接
    $qinfo['pcurl'] = self::$askPcUrl.$qinfo['id'].'-p1.html'; //pc版鏈接
    $xml.=$this->askMapMobileUrl($qinfo); //移動版
    $xml.=$this->askMapPcUrl($qinfo);     //pc版
    }
    $maxid = end($list);
    $indexArr['maxid'] = $maxid['id'];
    //更新索引文件
    if($bs==0){
    //更新最後一行
    $txt = file($index);
    $txt[count($txt)-1] = $indexArr[filename].','.$indexArr[maxid].','.$indexArr['minid'].','.$indexArr['maxXml']."rn";
    $str = join($txt);
    if (is_writable($index)) {
    if (!$handle = fopen($index, 'w')) {
    echo "不能打開文件 $index";exit;
    exit;
    }
    if (fwrite($handle, $str) === FALSE) {
    echo "不能寫入到文件 $index";exit;
    exit;
    }
    echo "成功地寫入文件$index";
    fclose($handle);
    } else {
    echo "文件 $index 不可寫";exit;
    }
    fclose($index);
    }elseif($bs==1){
    //新加入一行
    $fp = fopen($index,'a');
    $num = count($list);
    $string = $indexArr[filename].','.$indexArr[maxid].','.$indexArr['minid'].','.$num."rn";
    if(fwrite($fp,$string)===false){
    echo "追加新行失敗。。。";exit;
    }else{
    echo "追加成功<br />";
    //更新sitemap索引文件
    $xmlData="<?xml version="1.0"  encoding="UTF-8" ?>".chr(10);
    $xmlData.="<sitemapindex>".chr(10);
    $xmlData.="</sitemapindex>";
    if(!file_exists($askXml))
    file_put_contents($askXml,$xmlData);
    $fileList = file($askXml);
    $fileCount = count($fileList);
    $setmapxml = "http://www.xxx.cn/ask/setmapxml/{$filename}.xml";//正常問題鏈接
    $txt = $this->setMapIndex($setmapxml);
    $fileList[$fileCount-1]=$txt."</sitemapindex>";
    $newContent = '';
    foreach($fileList as $v){
    $newContent.= $v;
    }
    if(!file_put_contents($askXml,$newContent)) exit('無法寫入數據');
    echo '已經寫入文檔' . $askXml;
    }
    fclose($fp);
    }
    $filename =  APP_PATH.'setmapxml/'.$filename.'.xml';
                    //更新到xml文件中,增加結尾
                    if(!file_exists($filename))
                            file_put_contents($filename,$start);
                    $xmlList = file($filename);
                    $xmlCount = count($fileList);
                    $xmlList[$xmlCount-1]=$xml."</urlset>";
                    $newXml = '';
    foreach($xmlList as $v){
    $newXml.= $v;
    }
    if(!file_put_contents($filename, $newXml))exit("寫入數據錯誤");
                    else
                        echo "寫入數據成功<br />";
    }
    //問答移動版xml
    private function askMapMobileUrl($data){
    $xml = '';
    if(is_array($data)&&!empty($data)){
    $xml .="<url>".chr(10);
    if($data['id'])
    $xml.='<loc>'.$data['mobielurl'].'</loc>'.chr(10);//移動版鏈接
    $xml.="<mobile:mobile type="mobile"/>".chr(10);
    if($data['lastmod'])
    $xml.='<lastmod>'.$data['lastmod'].'</lastmod>'.chr(10);
    $xml.='<changefreq>daily</changefreq>'.chr(10);
    $xml.='<priority>0.8</priority>'.chr(10);  
    $xml.="</url>".chr(10);
    return $xml;
    }
    }
    //問答pc版xml
    private function askMapPcUrl($data){
    $xml = '';
    if(is_array($data)&&!empty($data)){
    $xml.='<url>'.chr(10);
    if($data['id'])
    $xml.='<loc>'.$data['pcurl'].'</loc>'.chr(10);//pc版鏈接
    if($data['lastmod'])
    $xml.='<lastmod>'.$data['lastmod'].'</lastmod>'.chr(10);
    $xml.='<changefreq>daily</changefreq>'.chr(10);
    $xml.='<priority>0.8</priority>'.chr(10);
    $xml.='</url>'.chr(10);
    return $xml;
    }
    }
    //setmaps索引文件
    private function setMapIndex($filename){
    $xml = '';
    $xml.="<sitemap>".chr(10);
    $xml.="<loc>{$filename}</loc>".chr(10);
    $xml.="<lastmod>".date("Y-m-d",time())."</lastmod>".chr(10);
    $xml.="</sitemap>".chr(10);
    return $xml;
    }
    }
    ?>


     
    xml索引文件格式如下:

     

     

    代碼如下: <?xml version="1.0"  encoding="UTF-8" ?>
    <sitemapindex>
    <sitemap>
    <loc>http://www.xxx.cn/ask/setmapxml/0.xml</loc>
    <lastmod>2014-05-12</lastmod>
    </sitemap>
    <sitemap>
    <loc>http://www.xxx.cn/ask/setmapxml/1.xml</loc>
    <lastmod>2014-05-12</lastmod>
    </sitemap>
    </sitemapindex>


     
    xml文件格式(每個文件需要存儲5000條、現展示1條例子)

    代碼如下: <?xml version="1.0" encoding="UTF-8" ?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">
    <url>
    <loc>http://m.xxx.cn/ask/7460.html</loc>
    <mobile:mobile type="mobile"/>
    <lastmod>2013-01-11</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
    </url>
    <url>
    </urlset>


     
    至於sql代碼主要就是一個select語句,這裡就不貼出來了。

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved