索引格式sitemap.xml生成类 持续写入 批量写入 保存更新数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27905477/article/details/80480446
<?php

/**`这里写代码片`
 *  Author: 老家的回忆
    Time : 2018.05.26
    Discription: 主要应用网站优化,此版本课兼容最大承受写入量为49990*1000;至于为什么百度站长平台有说!
    偶也是灰灰,望有大神一起学习探讨!qq:740453714
 */
class sitemap{

    private $resconns;//定义写入url的结果集
    private $sitemappath ="../sitemap.xml";//定义索引写入路径
    public $arrmaxurl;//将写入url的每个栏目最大值存入数据库 类型数组 包含各存储表的键名

    public function wsuoyinxml($sitemappath,$resconns,$arrmaxurl){
            if(file_exists($sitemappath)){
            $ressuoyin = file_get_contents($sitemappath);
            return existsloc($ressuoyin,$resconns,$arrmaxurl);
        }
    }

    // 索引格式参考
    /**
     * 
        <?xml version="1.0" encoding="UTF-8"?>
        <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <sitemap>
        ...
        </sitemap>
        <sitemap>
        <loc>https://www.1586f.com/xml/site1.xml</loc>
        <lastmod>2018-05-26 12:04:00</lastmod>
        </sitemap>
        </sitemapindex>

     */

    /**
     * xmlurl文件格式参考
        <?xml version="1.0" encoding="UTF-8"?>
        <urlset>
        <url>
        <loc>https://www.1586f.com</loc>
        <priority>1.0</priority>
        <lastmod>2018-05-20</lastmod>
        <changefreq>always</changefreq>
        </url>
        </urlset>


     */

    /**
     * 说明 PHP_EOL,表示换行符自动兼容moc linux windows
     */

    public function existsloc($ressuoyin,$resconns,$arrmaxurl){
            $strstart = strrpos($ressuoyin, "</loc>");
            $strlocend = strrpos($ressuoyin, "<loc>");
            $fileurl = substr($ressuoyin,$strlocend+5,$strstart-121);
            $xiegang = "../xml".strrchr($fileurl,"/");
            // echo $xiegang;
            return wsitemapxml($xiegang,$resconns,$arrmaxurl);



    }


    public function wsitemapxml($sitemapurl,$resconns,$arar){
    if(file_exists($sitemapurl)){
        $redfilecon = file_get_contents($sitemapurl);
        $redfileconlen = strlen($redfilecon);
        $deleteurlset = substr($redfilecon, 0,$redfileconlen-9);
        $countloc = substr_count($deleteurlset, "<loc>");//获取文件中url数量
        $resconnnum = substr_count($resconns, "<loc>");//获取写入结果集的数量
        $loccountnums = $countloc+$resconnnum; // 加入完全写入
//      echo $sitemapurl."<br>";
//      echo $loccountnums."<br>";
//      echo $resconns."<br>";
// var_dump($arar)."<br>";
//      die;
        if($loccountnums>49990){

            //直接创建新文件写入
            $basename = basename($sitemapurl);
            $xmlfilexuhao = preg_match("[\d*]",$basename);//获取字符成功
            // // 写入文件名
            $xmlfilexuhaojia =  $xmlfilexuhao+1;
            $newfilename = "../xml/site".$xmlfilexuhaojia.".xml";
            fopen($newfilename, "w");//创建文件
            fclose($newfilename);
            if(file_exists($newfilename)){
                $newsitejiacon = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".PHP_EOL."<urlset>".PHP_EOL.$resconns."</urlset>";
                file_put_contents($newfilename,$newsitejiacon);
                    $suoyinfile = "../sitemap.xml";
                    $suoyinfilecon = file_get_contents($suoyinfile);
                    $suoyinstr = "<sitemap>".PHP_EOL."<loc>https://www.1586f.com/xml/site".$xmlfilexuhaojia.".xml</loc>".PHP_EOL."<lastmod>".date('Y-m-d H:i:s')."</lastmod>".PHP_EOL."</sitemap>".PHP_EOL."</sitemapindex>";
                    $addsitemaploc = str_replace("</sitemapindex>",$suoyinstr, $suoyinfilecon);

                    file_put_contents($suoyinfile, $addsitemaploc);
                    if(0<substr_count(file_get_contents($newfilename), "<loc>")){
                    /**
                     * 插入结果sql用来记录你的写入记录最大值,
                     */

                    mysql_query($sqlinterset);
                    if(mysql_affected_rows()>0){
                        echo "创建路径为".$newfilename."的文件! xml表记录更新成功"."<br />"."sitemap.xml更新成功!";
                    }else{
                        echo "创建路径为".$newfilename."的文件! xml表记录插入失败"."<br />"."sitemap.xml更新成功!";
                    }
                }
            }

        }else{

        //  //在49000之上且小于49900可以一次性写入
            $urlleftcount = substr_count($deleteurlset,"<url>");
            $urlrightcount = substr_count($deleteurlset, "</url>");
            if(isset($urlleftcount) && isset($urlrightcount) && $urlleftcount==$urlrightcount){
                file_put_contents($sitemapurl, $deleteurlset.$resconns."</urlset>");//文件小直接写入
                    if($countloc<substr_count(file_get_contents($sitemapurl), "<loc>")){

                        /**
                     * 插入结果sql用来记录你的写入记录最大值,
                     */

                    $sqlinterset = "Insert into zzcms_writsitemap(zx,main,pp,tv,vip,wsdk,wssp,wsyx,zh,zhcompany,zhzixun,dateday) values('".$arar["zxmaxid"]."','".$arar["zsmaxid"]."','".$arar["ppmaxid"]."','".$arar["tvmaxid"]."','".$arar["vipmaxid"]."','".$arar["wsdkmaxid"]."','".$arar["wsspmaxid"]."','".$arar["wsyxmaxid"]."','".$arar["zhmaxid"]."','".$arar["zhcompanymaxid"]."','".$arar["zhzixunmaxid"]."','".date('Y-m-d H:i:s')."')";
                    mysql_query($sqlinterset);
                    if(mysql_affected_rows()>0){
                        echo "xml表记录更新成功"."<br />"."sitemap.xml更新成功!";

                    }else{
                        echo "表记录插入失败"."<br />"."sitemap.xml更新成功!";
                    }
                }
            }

        }
    }
}


}

猜你喜欢

转载自blog.csdn.net/qq_27905477/article/details/80480446
今日推荐