PHP generated Baidu sitemap Site Map class Function Example _php tutorial

Source: Internet
Author: User
Tags map class

PHP generated Baidu sitemap Site Map class function instance


Problem Overview:

The company's website is a question and answer Encyclopedia of the website, SEO engineers put forward the need to say according to the site's problems to generate XML files. Each XML file contains 5,000 Setmap format data. There are now about 70w online sites, so basically generate 140 XML files. There is also an index file. For example, the name of a file begins with a number. The index file contains the path and name of each XML file.
Why do you want to store 5,000 data per file, because this is a boundary value for MySQL, and if you take more than one, you may have an impact on online user access, or slow down. Each file stores 5,000 data, but MySQL Selsect can not fetch 5,000 at a time, now write is to fetch 1000 each time. So the logic is a little complicated.

Implementation method:

First take out 1000 data (can be flexible to survive, easy to modify later), and then loop to generate XML format file. File_puts_contens writes the file. And then the generated XML file name, the minimum ID to take out the problem, the maximum ID to take out the problem, the number of the problem to get out of an index query TXT file, the format is probably this way.
0,3146886,3145887,1000
Found that the last side of the number is 1000, the first select to remove 1000 data, and then write to the 0.xml file. Writes the extracted XML file name, the minimum ID, the maximum ID, and the number of bars to the index query txt. 1000 data was written to 0.xml for the first time, and the number of generated bars was 1000. When the second query is made, the SELECT statement becomes. The maximum ID of where ID > fetched (current MySQL is a positive order query, if reversed, changed to less than) limit 1000 then take out 1000, then modify the index query TXT minimum ID, maximum ID, the number of generated bars plus 2000. And so on, when the number of generated bars is 5000, another line is written to the index file, like this
0,3146886,3145887,5000
1,3148886,3147887,1000
This will relieve the pressure on the server.
The implementation code is posted below (style is a bit messy):

The specific function code is as follows:

The code is as follows:

/*
* Sitemap Interface class
*/

Class Sitemapaction extends action{
private static $baseURL = '; URL address
private static $ASKMOBILEURL = ' http://m.xxx.cn/ask/'; Question and Answer mobile address
private static $ASKPCURL = "http://www.xxx.cn/ask/"; Q/A PC address
private static $ASKZONEPCURL = "http://www.xxx.cn/ask/jingxuan/"; Quiz Featured PC Links
private static $ASKZONEMOBILEURL = "http://m.xxx.cn/ask/jx/"; Quiz Featured Mobile Links
Question and Answer Setmaps
Public Function Asksetmap () {
Header (' Content-type:text/html;charset=utf-8 ');
Get a list of issues
$maxid = 0; Index file Maximum ID
$minid = 0; Index file Minimum ID
$psize = 1000; Number of databases per fetch
$MAXXML = 5000; Number of XML Write records
$where = Array ();
Reading index files
$index = App_path. ' Setmapxml/index.txt ';
Associating setmaps paths
$ASKXML = ". /siteditu/ask/ask.xml ";
if (!file_exists ($index)) {
$FP =fopen ("$index", "w+");
if (!is_writable ($index)) {
Die ("File:". $index. " Not writable, please check! ");
}
Fclose ($FP);
}else{
Index.txt File Description 0:xml file name (starting from 1), 1: File Maximum ID, 2: File Minimum ID, 3: File current Record Count
$fp = file ($index);
$string = $fp [Count ($fp) -1];//displays the last line
$arr = Explode (', ', $string);
}
Whether the number of index files is less than $maxxml
If you are running for the first time
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 "File name:". $filename. ". XML "."
";
echo "Maximum ID:" $maxid. "
";
echo "Minimum ID:" $minid. "
";
echo "XML writes maximum record:". $maxXml. "
";
echo "Database per read:". $psize. "
";
$list = self:: $QUESTIONOBJ->getquestionsetmap ($where, $maxid, $psize);
if (count ($list) <=0) {
Echo 1;exit;
}
$record = $arr [3]+count ($list); Index file Write Record count
$INDEXARR = Array (' filename ' = = $filename, ' maxid ' + $maxid, ' minid ' = $minid, ' maxxml ' + $record);
$start = ' '. Chr (10);
$start. = " ". Chr (10);
$start. = " ";
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 '; Mobile Version Link
$qinfo [' pcurl '] = self:: $askPcUrl. $qinfo [' id ']. ' -p1.html '; PC version link
$xml. = $this->askmapmobileurl ($qinfo); Mobile version
$xml. = $this->askmappcurl ($qinfo); PC version
}
$maxid = End ($list);
$INDEXARR [' maxid '] = $MAXID [' id '];
Update index file
if ($bs ==0) {
Update last line
$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 "Cannot open file $index"; exit;
Exit
}
if (fwrite ($handle, $str) = = = = FALSE) {
echo "Cannot write to file $index"; exit;
Exit
}
echo "successfully wrote to file $index";
Fclose ($handle);
} else {
echo "File $index not writable"; exit;
}
Fclose ($index);
}elseif ($bs ==1) {
Add a new row
$fp = fopen ($index, ' a ');
$num = count ($list);
$string = $INDEXARR [filename]. ', '. $INDEXARR [Maxid]. ', '. $indexArr [' MiniD ']. ', '. $num. ' \ r \ n ";
if (fwrite ($fp, $string) ===false) {
echo "Append New Line Failed ... "; exit;
}else{
echo "Append success
";
Update the sitemap index file
$xmlData = " ". Chr (10);
$xmlData. = " ". Chr (10);
$xmlData. = " ";
if (!file_exists ($ASKXML))
File_put_contents ($ASKXML, $xmlData);
$fileList = file ($askXml);
$fileCount = count ($fileList);
$setmapxml = "http://www.xxx.cn/ask/setmapxml/{$filename}.xml";//Normal problem link
$txt = $this->setmapindex ($setmapxml);
$fileList [$fileCount -1]= $txt.]";
$newContent = ";
foreach ($fileList as $v) {
$newContent. = $v;
}
if (!file_put_contents ($ASKXML, $newContent)) exit (' cannot write data ');
Echo ' has been written to the document '. $ASKXML;
}
Fclose ($FP);
}
$filename = App_path. ' setmapxml/' $filename. '. XML ';
Update to XML file, add end
if (!file_exists ($filename))
File_put_contents ($filename, $start);
$xmlList = file ($filename);
$xmlCount = count ($fileList);
$xmlList [$xmlCount -1]= $xml.]";
$NEWXML = ";
foreach ($xmlList as $v) {
$newXml. = $v;
}
if (!file_put_contents ($filename, $newXml)) exit ("Write data Error");
Else
echo "Write data successfully
";
}
Question and answer the mobile version of XML
Private Function Askmapmobileurl ($data) {
$xml = ";
if (Is_array ($data) &&!empty ($data)) {
$xml. = " ". Chr (10);
if ($data [' ID '])
$xml. = ' . $data [' Mobielurl ']. ' '. chr (10);//Mobile link
$xml. = " ". Chr (10);
if ($data [' lastmod '])
$xml. = ' . $data [' Lastmod ']. ' '. chr (10);
$xml. = ' daily '. Chr (10);
$xml. = ' 0.8 '. chr (10);
$xml. = " ". Chr (10);
return $xml;
}
}
Quiz PC version xml
Private Function Askmappcurl ($data) {
$xml = ";
if (Is_array ($data) &&!empty ($data)) {
$xml. = ' '. Chr (10);
if ($data [' ID '])
$xml. = ' . $data [' Pcurl ']. ' '. chr;//pc version link
if ($data [' lastmod '])
$xml. = ' . $data [' Lastmod ']. ' '. chr (10);
$xml. = ' daily '. Chr (10);
$xml. = ' 0.8 '. chr (10);
$xml. = ' '. Chr (10);
return $xml;
}
}
Setmaps Index File
Private Function Setmapindex ($filename) {
$xml = ";
$xml. = " ". Chr (10);
$xml. = " {$filename} ". chr (10);
$xml. = " ". Date ("Y-m-d", Time ()). " ". Chr (10);
$xml. = " ". Chr (10);
return $xml;
}
}
?>



The XML index file format is as follows:

The code is as follows:




Http://www.xxx.cn/ask/setmapxml/0.xml
2014-05-12


Http://www.xxx.cn/ask/setmapxml/1.xml
2014-05-12



XML file format (each file needs to store 5,000, now show 1 examples)

The code is as follows:




Http://m.xxx.cn/ask/7460.html

2013-01-11
Daily
0.8




As for the SQL code is mainly a SELECT statement, which is not posted here.

http://www.bkjia.com/PHPjc/896776.html www.bkjia.com true http://www.bkjia.com/PHPjc/896776.html techarticle PHP generated Baidu sitemap Site Map Class Function Example problem Overview: The company's website is a question and answer Encyclopedia of the website, SEO engineers put forward the need to say according to the site's problems to generate XML files. Every ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.