In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "php generates Baidu sitemap site map function". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
This paper describes the method of generating Baidu sitemap site map class function by php, which is shared with you for your reference. The specific implementation methods are as follows:
Overview of the problem:
The company's website is a question and answer encyclopedia website, seo engineers put forward the demand to generate xml files according to the questions of the website. Each xml file contains 5000 pieces of data in setmap format. At present, there are about 70w questions on the website online, so basically generate 140xml 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 store 5000 pieces of data per file, because this is a threshold for mysql, if you take too much each time, it may affect online user access, or slow down. Each file stores 5000 pieces of data, but when you mysql selsect, you can't take 5000 at a time, but now you can write 1000 at a time. Then the logic is a little complicated.
Implementation method:
First take out 1000 pieces of data (can be more flexible to survive, easy to modify later), and then cycle to generate xml format files. File_puts_contens writes to the file. Then write the name of the generated xml file, the minimum id of the problem, the maximum id of the problem, and the number of entries of the problem into an index query txt file, which looks like this.
0,3146886,3145887,1000
Found that the last number is 1000, the first time select took out 1000 pieces of data, and then wrote it into the 0.xml file. Write the name of the extracted xml file, the minimum id, the maximum id and the number of entries to the index query txt. 1000 pieces of data were written to 0.xml for the first time, and the number of generated entries was 1000. On the second query, the select statement becomes. Where id > the maximum id retrieved (the current mysql is positive order query, if the reverse order is changed to less than) limit 1000, then take out 1000, then modify the minimum id and maximum id of the index query txt, and increase the number of generated entries to 2000. And so on, when the number of generated entries reaches 5000, another line is written to the index file, like this.
0,3146886,3145887,5000
1,3148886,3147887,1000
Writing in this way relieves the pressure on the server.
The implementation code is posted below (the style is a bit messy):
The specific function codes are as follows:
The copy code is as follows:
$qinfo) {
If ($krypton0)
$indexArr ['minid'] = $qinfo [' id']
$qinfo ['lastmod'] = substr ($qinfo [' lasttime'], 0mai 10)
$qinfo ['mobielurl'] = self::$askMobileUrl.$qinfo [' id']. '.html'; / / Mobile link
$qinfo ['pcurl'] = self::$askPcUrl.$qinfo [' id'].'- p1.htmllinks; / / pc version link
$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 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 the file $index"
Fclose ($handle)
} else {
Echo "File $index is not writable"; exit
}
Fclose ($index)
} elseif ($bs==1) {
/ / add a new line
$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 new line." ; 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 question link
$txt = $this- > setMapIndex ($setmapxml)
$fileList [$fileCount-1] = $txt. ""
$newContent =''
Foreach ($fileList as $v) {
$newContent.= $v
}
If (! file_put_contents ($askXml,$newContent)) exit ('unable to write data')
Echo 'has been written to the document'. $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 ("write data error")
Else
Echo "write data successful"
}
/ / Mobile question and answer xml
Private function askMapMobileUrl ($data) {
$xml =''
If (is_array ($data) & &! empty ($data)) {
$xml. = ".chr (10)
If ($data ['id'])
$xml.=''.$data ['mobielurl'].' .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
}
}
/ / xml for Q & A pc
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 copy 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 5000 items, 1 example is shown)
The copy 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, it will not be posted here.
The content of "php generates Baidu sitemap site map class function" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.