午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    php輸出xml必須header的解決方法
    來(lái)源:易賢網(wǎng) 閱讀:1530 次 日期:2014-10-22 11:35:17
    溫馨提示:易賢網(wǎng)小編為您整理了“php輸出xml必須header的解決方法”,方便廣大網(wǎng)友查閱!

    問(wèn)題概述:

    公司網(wǎng)站是問(wèn)答百科的網(wǎng)站、seo工程師提出需求說(shuō)根據(jù)網(wǎng)站的問(wèn)題來(lái)生成xml文件。每個(gè)xml文件包含5000條setmap格式數(shù)據(jù)?,F(xiàn)在線上網(wǎng)站大約有70w條問(wèn)題,所以說(shuō)基本生成140個(gè)xml文件。還有一個(gè)索引文件。比如文件的名稱以數(shù)字開頭的。索引文件包含的內(nèi)容就是每個(gè)xml文件的路徑還有名稱。

    為什么要每個(gè)文件存儲(chǔ)5000條數(shù)據(jù)呢,因?yàn)檫@是mysql的一個(gè)界限值、如果每次取多了以后可能會(huì)對(duì)線上用戶訪問(wèn)造成影響,或者速度變慢。每個(gè)文件存儲(chǔ)5000條數(shù)據(jù),但是mysql selsect的時(shí)候不能每次取5000條、現(xiàn)在寫的是每次取1000條。那這樣邏輯就有點(diǎn)復(fù)雜。

    實(shí)現(xiàn)方法:

    首先取出1000條數(shù)據(jù)(可以靈活些成活的,方便以后修改),然后循環(huán)生成xml格式文件。file_puts_contens寫入文件。然后再把生成的xml文件名稱、取出問(wèn)題的最小id、取出問(wèn)題的最大id、取出問(wèn)題的條數(shù)寫出一個(gè)索引查詢的txt文件當(dāng)中,格式大概是這個(gè)樣子的。

    0,3146886,3145887,1000

    發(fā)現(xiàn)最后面的條數(shù)是1000了嗎、第一次select取出1000條數(shù)據(jù)、然后寫入0.xml文件當(dāng)中。把取出的xml文件名稱、最小id、最大id、條數(shù)寫入到索引查詢txt中。第一次寫入了1000條數(shù)據(jù)到0.xml、生成條數(shù)為1000。第二次查詢的時(shí)候select語(yǔ)句會(huì)成為。 where id > 取出的最大id(當(dāng)前mysql為正序查詢、如果為倒序、改成小于) limit 1000 這樣的話又取出1000、然后修改索引查詢txt的最小id、最大id、生成條數(shù)加到2000。以此類推等生成條數(shù)到了5000的時(shí)候再另起一行寫入索引文件、類似這樣

    0,3146886,3145887,5000

    1,3148886,3147887,1000

    這樣寫的話就減輕了服務(wù)器的壓力。

    下面貼出實(shí)現(xiàn)代碼(風(fēng)格有點(diǎn)亂):

    具體功能代碼如下:

    代碼如下:

    <?php

    /*

    * SiteMap接口類

    */

    class SitemapAction extends Action{

    private static $baseURL = ''; //URL地址

    private static $askMobileUrl = 'http://m.xxx.cn/ask/'; //問(wèn)答移動(dòng)版地址

    private static $askPcUrl = "http://www.xxx.cn/ask/"; //問(wèn)答pc地址

    private static $askZonePcUrl = "http://www.xxx.cn/ask/jingxuan/"; //問(wèn)答精選Pc鏈接

    private static $askZoneMobileUrl = "http://m.xxx.cn/ask/jx/"; //問(wèn)答精選移動(dòng)版鏈接

    //問(wèn)答setmaps

    public function askSetMap(){

    header('Content-type:text/html;charset=utf-8');

    //獲取問(wèn)題列表

    $maxid = 0; //索引文件最大id

    $minid = 0; //索引文件最小id

    $psize = 1000; //數(shù)據(jù)庫(kù)每次取數(shù)量

    $maxXml = 5000; //xml寫入記錄數(shù)量

    $where = array();

    //讀取索引文件

    $index = APP_PATH.'setmapxml/Index.txt';

    //關(guān)聯(lián)setmaps路徑

    $askXml = "../siteditu/ask/ask.xml";

    if(!file_exists($index)){

    $fp=fopen("$index", "w+");

    if ( !is_writable($index) ){

    die("文件:" .$index. "不可寫,請(qǐng)檢查!");

    }

    fclose($fp);

    }else{

    //index.txt文件說(shuō)明 0:xml文件名稱(從1開始)、1:文件最大id、2:文件最小id、3:文件當(dāng)前記錄數(shù)

    $fp = file($index);

    $string = $fp[count($fp)-1];//顯示最后一行

    $arr = explode(',', $string);

    }

    //索引文件數(shù)量是否小于$maxXml

    //如果為第一次運(yùn)行

    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 "數(shù)據(jù)庫(kù)每次讀取數(shù)量:".$psize."<br />";

    $list = self::$questionObj->getQuestionSetMap($where,$maxid,$psize);

    if(count($list)<=0){

    echo 1;exit;

    }

    $record = $arr[3]+count($list); //索引文件寫入記錄數(shù)

    $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'; //移動(dòng)版鏈接

    $qinfo['pcurl'] = self::$askPcUrl.$qinfo['id'].'-p1.html'; //pc版鏈接

    $xml.=$this->askMapMobileUrl($qinfo); //移動(dòng)版

    $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']."\r\n";

    $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."\r\n";

    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";//正常問(wèn)題鏈接

    $txt = $this->setMapIndex($setmapxml);

    $fileList[$fileCount-1]=$txt."</sitemapindex>";

    $newContent = '';

    foreach($fileList as $v){

    $newContent.= $v;

    }

    if(!file_put_contents($askXml,$newContent)) exit('無(wú)法寫入數(shù)據(jù)');

    echo '已經(jīng)寫入文檔' . $askXml;

    }

    fclose($fp);

    }

    $filename = APP_PATH.'setmapxml/'.$filename.'.xml';

    //更新到xml文件中,增加結(jié)尾

    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("寫入數(shù)據(jù)錯(cuò)誤");

    else

    echo "寫入數(shù)據(jù)成功<br />";

    }

    //問(wèn)答移動(dòng)版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);//移動(dòng)版鏈接

    $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;

    }

    }

    //問(wèn)答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文件格式(每個(gè)文件需要存儲(chǔ)5000條、現(xiàn)展示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代碼主要就是一個(gè)select語(yǔ)句,這里就不貼出來(lái)了。

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:php輸出xml必須header的解決方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)