Home Backend Development PHP Tutorial php生成百度sitemap站点地图类函数实例_PHP

php生成百度sitemap站点地图类函数实例_PHP

May 31, 2016 pm 07:29 PM
php generate Baidu sitemap

本文实例讲述了php生成百度sitemap站点地图类函数的方法,分享给大家供大家参考。具体实现方法如下:

问题概述:

公司网站是问答百科的网站、seo工程师提出需求说根据网站的问题来生成xml文件。每个xml文件包含5000条setmap格式数据。现在线上网站大约有70w条问题,所以说基本生成140个xml文件。还有一个索引文件。比如文件的名称以数字开头的。索引文件包含的内容就是每个xml文件的路径还有名称。
为什么要每个文件存储5000条数据呢,因为这是mysql的一个界限值、如果每次取多了以后可能会对线上用户访问造成影响,或者速度变慢。每个文件存储5000条数据,但是mysql selsect的时候不能每次取5000条、现在写的是每次取1000条。那这样逻辑就有点复杂。

实现方法:

首先取出1000条数据(可以灵活些成活的,方便以后修改),然后循环生成xml格式文件。file_puts_contens写入文件。然后再把生成的xml文件名称、取出问题的最小id、取出问题的最大id、取出问题的条数写出一个索引查询的txt文件当中,格式大概是这个样子的。
0,3146886,3145887,1000
发现最后面的条数是1000了吗、第一次select取出1000条数据、然后写入0.xml文件当中。把取出的xml文件名称、最小id、最大id、条数写入到索引查询txt中。第一次写入了1000条数据到0.xml、生成条数为1000。第二次查询的时候select语句会成为。 where id > 取出的最大id(当前mysql为正序查询、如果为倒序、改成小于) limit 1000 这样的话又取出1000、然后修改索引查询txt的最小id、最大id、生成条数加到2000。以此类推等生成条数到了5000的时候再另起一行写入索引文件、类似这样
0,3146886,3145887,5000
1,3148886,3147887,1000
这样写的话就减轻了服务器的压力。
下面贴出实现代码(风格有点乱):

具体功能代码如下:

代码如下:

/*
 * SiteMap接口类
 */
 
class SitemapAction extends Action{
private static $baseURL = ''; //URL地址
private static $askMobileUrl = 'http://m.xxx.cn/ask/'; //问答移动版地址
private static $askPcUrl = "http://www.xxx.cn/ask/";   //问答pc地址
private static $askZonePcUrl = "http://www.xxx.cn/ask/jingxuan/"; //问答精选Pc链接
private static $askZoneMobileUrl = "http://m.xxx.cn/ask/jx/"; //问答精选移动版链接
        //问答setmaps
public function askSetMap(){
header('Content-type:text/html;charset=utf-8');
//获取问题列表
$maxid = 0;    //索引文件最大id
$minid = 0;    //索引文件最小id
$psize = 1000; //数据库每次取数量
$maxXml = 5000; //xml写入记录数量
$where = array();
//读取索引文件
$index = APP_PATH.'setmapxml/Index.txt';
//关联setmaps路径
$askXml = "../siteditu/ask/ask.xml";
if(!file_exists($index)){
$fp=fopen("$index", "w+");
if ( !is_writable($index) ){
die("文件:" .$index. "不可写,请检查!");
}
fclose($fp);
}else{
//index.txt文件说明 0:xml文件名称(从1开始)、1:文件最大id、2:文件最小id、3:文件当前记录数
$fp = file($index);
$string = $fp[count($fp)-1];//显示最后一行
$arr = explode(',', $string);
}
//索引文件数量是否小于$maxXml
//如果为第一次运行
if(!$arr[1]){
$bs=1;
$filename=0;
}else{
if($arr && $arr[3]$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 "文件名称:".$filename.".xml"."
";
echo "最大id:".$maxid."
";
echo "最小id:".$minid."
";
echo "xml写入最大记录:".$maxXml."
";
echo "数据库每次读取数量:".$psize."
";
$list = self::$questionObj->getQuestionSetMap($where,$maxid,$psize);
if(count($list)echo 1;exit;
}
$record = $arr[3]+count($list); //索引文件写入记录数
$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'; //移动版链接
$qinfo['pcurl'] = self::$askPcUrl.$qinfo['id'].'-p1.html'; //pc版链接
$xml.=$this->askMapMobileUrl($qinfo); //移动版
$xml.=$this->askMapPcUrl($qinfo);     //pc版
}
$maxid = end($list);
$indexArr['maxid'] = $maxid['id'];
//更新索引文件
if($bs==0){
//更新最后一行
$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 "不能打开文件 $index";exit;
exit;
}
if (fwrite($handle, $str) === FALSE) {
echo "不能写入到文件 $index";exit;
exit;
}
echo "成功地写入文件$index";
fclose($handle);
} else {
echo "文件 $index 不可写";exit;
}
fclose($index);
}elseif($bs==1){
//新加入一行
$fp = fopen($index,'a');
$num = count($list);
$string = $indexArr[filename].','.$indexArr[maxid].','.$indexArr['minid'].','.$num."\r\n";
if(fwrite($fp,$string)===false){
echo "追加新行失败。。。";exit;
}else{
echo "追加成功
";
//更新sitemap索引文件
$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";//正常问题链接
$txt = $this->setMapIndex($setmapxml);
$fileList[$fileCount-1]=$txt."";
$newContent = '';
foreach($fileList as $v){
$newContent.= $v;
}
if(!file_put_contents($askXml,$newContent)) exit('无法写入数据');
echo '已经写入文档' . $askXml;
}
fclose($fp);
}
$filename =  APP_PATH.'setmapxml/'.$filename.'.xml';
                //更新到xml文件中,增加结尾
                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("写入数据错误");
                else
                    echo "写入数据成功
";
}
//问答移动版xml
private function askMapMobileUrl($data){
$xml = '';
if(is_array($data)&&!empty($data)){
$xml .="".chr(10);
if($data['id'])
$xml.=''.$data['mobielurl'].''.chr(10);//移动版链接
$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;
}
}
//问答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版链接
if($data['lastmod'])
$xml.=''.$data['lastmod'].''.chr(10);
$xml.='daily'.chr(10);
$xml.='0.8'.chr(10);
$xml.='
'.chr(10);
return $xml;
}
}
//setmaps索引文件
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;
}
}
?>


 
xml索引文件格式如下:

 

 

代码如下:




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


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


 
xml文件格式(每个文件需要存储5000条、现展示1条例子)

代码如下:




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

2013-01-11
daily
0.8



 
至于sql代码主要就是一个select语句,这里就不贴出来了。

 

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

See all articles