索引格式sitemap.xml生成類 持續寫入 批量寫入 儲存更新資料
阿新 • • 發佈:2019-01-14
<?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更新成功!";
}
}
}
}
}
}
}