Php generates a map function instance of the Baidu sitemap site and a map of the sitemap site. Php generates Baidu sitemap site map function instances. sitemap site map this article describes how php generates Baidu sitemap site map functions. Use php to generate Baidu sitemap site map function instances, sitemap site map
This article describes how to generate map functions for Baidu sitemap sites using php. The specific implementation method is as follows:
Problem Overview:
The company's website is a question-and-answer Encyclopedia website. seo engineers make demands to generate xml files based on website problems. Each xml file contains 5000 pieces of setmap data. There are about 140 million questions about online websites, so xml files are basically generated. There is also an index file. For example, the file name starts with a number. The index file contains the path and name of each xml file.
Why Store 5000 pieces of data per file? this is a mysql boundary value. if you get more data each time, it may affect online user access or slow down the access speed. 5000 pieces of data are stored in each file, but 5000 pieces of data cannot be obtained each time in mysql selsect. Currently, 1000 pieces are written each time. The logic is a bit complicated.
Implementation method:
First, take out 1000 pieces of data (which can be flexible and easy to modify later) and generate xml format files cyclically. File_puts_contens writes data to the file. Then, write the generated xml file name, the minimum id of the problem to be retrieved, the maximum id of the problem to be retrieved, and the number of items to be retrieved into the txt file for index query, the format is like this.
0,3146886, 3145887,1000
Is it found that the number of the last lines is 1000? for the first time, select 1000 data records and write them into the 0. xml file. Write the retrieved xml file name, minimum id, maximum id, and number of entries to the index query txt file. 1000 data records are written to 0.xmlfor the first time, and the number of generated records is 1000. In the second query, the select statement becomes. Where id> The largest Retrieved id (current mysql is a forward query, if it is a reverse order, change to less) in limit 1000, 1000 is taken out again, and then the minimum id and maximum id of the index query txt are modified, and the number of generated entries is increased to 2000. Similarly, when the number of generated entries reaches 5000, another row is written to the index file.
0,3146886, 3145887,5000
1, 3148886, 3147887,1000
This reduces the pressure on the server.
The following is the implementation code (style is messy ):
The specific function code is as follows:
The code is as follows:
<? Php
/*
* SiteMap interface class
*/
Class SitemapAction extends Action {
Private static $ baseURL = ''; // URL
Private static $ askMobileUrl = 'http: // m.xxx.cn/ask/'; // mobile address of the Q &
Private static $ askPcUrl = "http://www.xxx.cn/ask/"; // Q & A pc address
Private static $ askZonePcUrl = "http://www.xxx.cn/ask/jingxuan/"; // featured Pc links
Private static $ askZoneMobileUrl = "http://m.xxx.cn/ask/jx/"; // Q & A featured mobile link
// Question and answer setmaps
Public function askSetMap (){
Header ('content-type: text/html; charset = utf-8 ');
// Obtain the question list
$ Maxid = 0; // The maximum id of the index file.
$ Minid = 0; // minimum index file id
$ Psize = 1000; // number of databases Retrieved each time
$ MaxXml = 5000; // number of write records in xml
$ Where = array ();
// Read the index file
$ Index = APP_PATH. 'setmapxml/Index.txt ';
// Associate the setmaps path
$ AskXml = "../siteditu/ask. xml ";
If (! File_exists ($ index )){
$ Fp = fopen ("$ index", "w + ");
If (! Is_writable ($ index )){
Die ("File:". $ index. "cannot be written. Please check it! ");
}
Fclose ($ fp );
} Else {
// Index.txt file description 0: xml file name (starting from 1), 1: Maximum File id, 2: Minimum File id, 3: current file records
$ Fp = file ($ index );
$ String = $ fp [count ($ fp)-1]; // display the last row
$ Arr = explode (',', $ string );
}
// Check whether the number of index files is less than $ maxXml
// If it is run 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 "Max id:". $ maxid ."
";
Echo "minimum id:". $ minid ."
";
Echo "maximum xml write Record:". $ maxXml ."
";
Echo "number of database reads per time:". $ psize ."
";
$ List = self: $ questionObj-> getQuestionSetMap ($ where, $ maxid, $ psize );
If (count ($ list) <= 0 ){
Echo 1; exit;
}
$ Record = $ arr [3] + count ($ list); // number of records written to the index file
$ IndexArr = array ('filename' => $ filename, 'maxid' => $ maxid, 'minid' => $ minid, 'maxxml' => $ record );
$ Start = '<? Xml version = "1.0" encoding = "UTF-8"?> '. Chr (10 );
$ Start. =" ". Chr (10 );
$ Start. =" ";
Foreach ($ list as $ k => $ qinfo ){
If ($ k = 0)
$ IndexArr ['minid'] = $ qinfo ['id'];
$ Qinfo ['lastmod'] = substr ($ qinfo ['lasttime );
$ Qinfo ['mobilelurl'] = self: $ askmobileurl.?qinfo='id'{.'html '; // mobile version link
$ Qinfo ['pcurl'] = self: $ askpcurl.?qinfo='id'{.'-p1.html '; // link to the PC version
$ Xml. = $ this-> askMapMobileUrl ($ qinfo); // Mobile version
$ Xml. = $ this-> askMapPcUrl ($ qinfo); // PC version
}
$ Maxid = end ($ list );
$ IndexArr ['maxid'] = $ maxid ['id'];
// Update the index file
If ($ bs = 0 ){
// Update the last row
$ 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 "the file $ index cannot be opened"; exit;
Exit;
}
If (fwrite ($ handle, $ str) = FALSE ){
Echo "cannot be written to the file $ index"; exit;
Exit;
}
Echo "successfully write the file $ index ";
Fclose ($ handle );
} Else {
Echo "file $ index cannot be written"; 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 "failed to append the new row... "; Exit;
} Else {
Echo "append successful
";
// Update the sitemap index file
$ XmlData = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ". 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 ('data cannot be written ');
Echo 'documented already '. $ askXml;
}
Fclose ($ fp );
}
$ Filename = APP_PATH. 'setmapxml/'. $ filename.'. XML ';
// Update to the xml file and add the 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 ("data writing error ");
Else
Echo "data written successfully
";
}
// Q & A mobile xml
Private function askMapMobileUrl ($ data ){
$ Xml = '';
If (is_array ($ data )&&! Empty ($ data )){
$ Xml. =" ". Chr (10 );
If ($ data ['id'])
$ Xml. =' '. $ Data ['mobilelurl'].' '. Chr (10); // mobile version 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;
}
}
// Q & A (PC) xml
Private function askMapPcUrl ($ data ){
$ Xml = '';
If (is_array ($ data )&&! Empty ($ data )){
$ Xml. =' '. Chr (10 );
If ($ data ['id'])
$ Xml. =' '. $ Data ['pcurl'].' '. Chr (10); // 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:
<? Xml version = "1.0" encoding = "UTF-8"?>
Http://www.xxx.cn/ask/setmapxml/0.xml </loc>
2014-05-12
Http://www.xxx.cn/ask/setmapxml/1.xml </loc>
2014-05-12
Xml file format (5000 entries are required for each file and one example is displayed)
The code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
Http://m.xxx.cn/ask/7460.html </loc>
2013-01-11
Daily
0.8
As for the SQL code, it is mainly a select statement, which will not be posted here.
I hope this article will help you with php programming.
Sitemapphp website map generation XML
Create a sitemap generation template and use the program to generate an XML file.
How does Zhimeng cms generate website map sitemap?
Go to the weaving dream background, click generate, and then update the website map in the last and fourth places on the left. click start update on the right. The website map is generated under the/datadirectory by default.
Examples in this article describe how php generates map functions for Baidu sitemap sites and share them with you for your reference. With...