opcache PHP新的字节码缓存扩展详解
字节码缓存组件 Zend Optimizer+ 现在更改名字为 Zend opcache了。且在php 5.5版本后,会集成到php的官方组件中,也就没有必要安装其他的APC,eAccelerator等了。。
APC与Opcache都是字节码缓存也就是,PHP在被编译的时候,首先会把php代码转换为字节码,字节码然后被执行。
php文件第二次执行时,同样还是会重新转换为字节码,但是很多时候,文件内容几乎是一样的,比如静态HTML文件,生成后内容许久都不会改变,用户访问请求直接由服务器读取响应给客户端浏览器。都不用经过PHP进行解析构建了。
内存中的字节码数据,可以直接缓存进行二次编译。这样程序就会快一些,cpu的消耗也少了。
详细介绍看这两篇
新一代 PHP 加速插件 Zend Opcache
php的 zend opcache VS apc 性能比较
我主要是用来测试了一下phpcmsV9.5.4 的默认index.php主页,没有数据内容,但有sql查询操作
测试是Apache2.2.6 32Bit 服务器,PHP 5.4.26 ts,mysql 5.6.16 64Bit
ab 版本 This is ApacheBench, Version 2.3 <$Revision: 655654 $>
请求数:1000
并发数:10
没有加载opcache测试
第一次测试
吞吐率有所提高 40.73 rps,耗时 24.554 s,每个请求耗时245.544 ms
opcache配置信息
1 |
opcache.memory_consumption=128 |
2 |
opcache.interned_strings_buffer=8 |
3 |
opcache.max_accelerated_files=4000 |
4 |
opcache.revalidate_freq=60 |
5 |
opcache.fast_shutdown=1 |
6 |
opcache.enable_cli=1 |
例如:
<?php
header('Content-type:text/html; charset=utf-8');
$str = 'abc';
echo $str; // 输出abc
?>
赋予$str 一个新的值
<?php
header('Content-type:text/html; charset=utf-8');
$str = 'new abc';
echo $str; // 输出的还是 abc
?>
这是因为$str 已经被编译为字节码了,再次访问时,内存里面还是可以找到对应的缓存,就没有进行重新编译,返回的值也就还是之前的值 abc
不过,opcache有一个参数可以用来设置缓存时间长度
opcache.force_restart_timeout (default "180")
默认时间为180秒,还是建议本地不用开启opcache
注意:官方给了一个Note,如果opcache要与xdebug同时加载,那么opcache需要在xdebug之前进行加载。
但是我本地测试了一下,xdebug先加载,再加载opcache,也正常启动了,xdebug,opcache都加载成功,opcache缓存也正常。
不过还是按照官方的建议来安装加载,否则可能会出现奇怪的错误吧
同步:
对象的阻塞模式和阻塞函数调用
对象是否处于阻塞模式和函数是不是阻塞调用有很强的相关性,但是并不是一一对应的。阻塞对象上可以有非阻塞的调用方式,我们可以通过一定的API去轮询状
2. 异步,就是我调用一个功能,不需要知道该功能结果,该功能有结果后通知我(回调通知)
3. 阻塞,
4. 非阻塞,
异步:请求通过事件触发->服务器处理(这是浏览器仍然可以作其他事情)->处理完毕
阻塞和非阻塞是指当进程访问的数据如果尚未就绪,进程是否需要等待,简单说这相当于函数内部的实现区别,也就是未就绪时是直接返回还是等待就绪;
2)非阻塞I/O (nonblocking I/O)
3) I/O复用(select 和poll) (I/O multiplexing)
4)信号驱动I/O (signal driven I/O (SIGIO))
5)异步I/O (asynchronous I/O (the POSIX aio_functions))
异步IO不会引起进程阻塞。
IO复用是先通过select调用阻塞。
2、效率提升,不是轮询的方式,不会随着FD数目的增加效率下降。只有活跃可用的FD才会调用callback函数;
select
|
单个进程所能打开的最大连接数有FD_SETSIZE宏定 义,其大小是32个整数的大小(在32位的机器上,大小就是32*32,同理64位机器上FD_SETSIZE为32*64),当然我们可以对进行修改, 然后重新编译内核,但是性能可能会受到影响,这需要进一步的测试。
|
poll
|
poll本质上和select没有区别,但是它没有最大连接数的限制,原因是它是基于链表来存储的
|
epoll
|
虽然连接数有上限,但是很大,1G内存的机器上可以打开10万左右的连接,2G内存的机器可以打开20万左右的连接
|
select
|
因为每次调用时都会对连接进行线性遍历,所以随着FD的增加会造成遍历速度慢的“线性下降性能问题”。
|
poll
|
同上
|
epoll
|
因为epoll内核中实现是根据每个fd上的 callback函数来实现的,只有活跃的socket才会主动调用callback,所以在活跃socket较少的情况下,使用epoll没有前面两者 的线性下降的性能问题,但是所有socket都很活跃的情况下,可能会有性能问题。
|
select
|
内核需要将消息传递到用户空间,都需要内核拷贝动作
|
poll
|
同上
|
epoll
|
epoll通过内核和用户空间共享一块内存来实现的。
|
DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量,不需要任何定义与包含即可直接使用。
众所周知,在windows下路径分隔符是(当然/在部分系统上也是可以正常运行的),在linux上路径的分隔符是/,这就导致了一个问题,比如开发机器是windows,有一个图片上传程序,调试机器上指定的上传文件保存目录是:define(‘ROOT’, dirname(__FILE__).”upload”),在本地调试都很正常,但是上传到linux服务器的时候会发现会出错。
这个问题就是出在文件的分隔符上,windows上习惯性的使用作为文件分隔符,但是在linux上人家是不认识这个标识的,人家只认识/,于是就要引入下面这个php内置变量了:DIRECTORY_SEPARATOR。
上面的写法可以改写为以下无错写法:
define(‘ROOT’, dirname(__FILE__).DIRECTORY_SEPARATOR.”upload”);
这样就可以确保不会出错了。
例如discuz里面是这样写的:define(‘S_ROOT’, dirname(__FILE__).DIRECTORY_SEPARATOR);
回到问题本身上,DIRECTORY_SEPARATOR是一个返回跟操作系统相关的路径分隔符的php内置命令,在windows上返回,而在linux或者类unix上返回/,就是这么个区别,通常在定义包含文件路径或者上传保存目录的时候会用到
。
define('ROOT', dirname(__FILE__)."\upload");
在本地调试都很正常,但是上传到linux服务器后就会出错。所以如上代码严谨的写法为:
define('ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR."upload");
提示:可以用函数get_defined_constants()来获取所有PHP常量,例如:
print_r(get_defined_constants());//get_defined_constants()返回所有常量数组
json_encode函数对于中文的操作不行同了,如果是uft8下还会碰到中文转成u590fu5a03u7684u8bf1u60d这种字符了,那么我们要如何输出成中文呢,下面来看看。
最近使用json_encode转换数组为json数据,储存在数据库里面,因为字段的长度个内容不确定,就只能使用这个方法了,但是使用json_decode解析为数组以后,却出现了类
似”u590fu5a03u7684u8bf1u60d14u5979u7684u6280u5de7″,通过查询百度,这应该是UCS-2编码的字符串,那么如何转换这个字符串呢?
其实在在php5.2以前的版本中做json_encode转换的时候的时候。中文会被unicode编码, php5.3加入了options参数, 5.4以后才加入JSON_UNESCAPED_UNICODE,这个参数,不需要做escape和unicode处理。 所以在5.4之前都需要对中文做个处理。
php5.4里面的处理
json_encode($str, JSON_UNESCAPED_UNICODE);
php5.4之前,有两种方法处理
方法一
function encode_json($str){ return preg_replace("/u([0-9a-f]+)/ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code); }
在实际应用中有个问题,部分字符会掉,不止为何,如字符串:”日期11.2″会被变成”日期.2″。
方法二
function encode_json($str) { return urldecode(json_encode(url_encode($str))); } function url_encode($str) { if(is_array($str)) { foreach($str as $key=>$value) { $str[urlencode($key)] = url_encode($value); } } else { $str = urlencode($str); } return $str; }
本站使用的是虚拟主机,就没法修改php的版本了,所以就只能采用第一种方法了,不过方法确实还是有效果的。
方法三 function decodeUnicode($str){ return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' ), $str); }
生成RSS非常的简单只需要使用rss格式然后生成输出到浏览器就可以了,重点就是要告诉浏览器你是rss格式的数据即可,具体来看个例子。
rss(简易信息聚合也叫聚合内容)是一种描述和同步网站内容的格式。下面的生成RSS订阅的代码:
rss XML结构
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>网站名称</title> <link>http://www.111cn.net/</link> <description>网站描述!</description> <item> <title>RSS Tutorial</title> <link>网站地址/rss</link> <description>New RSS tutorial on W3School</description> </item> <item> <title>XML Tutorial</title> <link>网站地址/xml</link> <description>New XML tutorial on W3School</description> </item> </channel> </rss>
RSS实例
<?php class Rss { public function createFeed() { //RSS头部 $webUrl = 'http://'.$_SERVER['HTTP_HOST'];//网站地址 $webName = '网站名称'; //网站名称 $webDesc = '网站描述'; //网站描述 $html = '<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>'.$webName.'</title> <link>'.$webUrl.'</link> <description>'.$webDesc.'</description> '.$this->createItem().' </channel> </rss> '; echo $html; } private function createItem() { //RSS item //$data可替换为自己的数据 $html = ''; //文章数据 $data = array( 'id' => 1, 'date' => date('r', time()), 'title' => '文章标题', 'link' => 'http://www.111cn.net', //文章地址 'description' => '网站描述' ); for($i = 0; $i < 6; $i++) { $html .= ' <item> <title>'.$data['title'].'</title> <link>'.$data['link'].'</link> <pubDate>'.$data['date'].'</pubDate> <description><![CDATA['.$data['description'].']]></description> </item> '; } return $html; } } header("Content-Type: text/xml; charset=utf-8"); $rss = new Rss(); $rss->createFeed(); exit; ?>
RSS Feed 生成后,如何设置才能给网站添加 RSS 呢?并且让 Firefox、IE7 或其它 Feed 机器人自动发现?很简单,在网页的 Head 节添加一个特定的 Link 标签即可,如下:
<link rel=”alternate” type=”application/rss+xml” title=”网站名称 RSS Feed” href=”http://www.111cn.net” />
设置 title 为 Feed 标题,href 为 Feed 地址,一切就 OK 了!
例子2
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>php程序员教程网</title> <link>http://www.111cn.net/</link> <description>本站是一个php程序员的工作生活笔记!</description> <item> <title>RSS Tutorial</title> <link>网站地址/rss</link> <description>New RSS tutorial on W3School</description> </item> <item> <title>XML Tutorial</title> <link>网站地址/xml</link> <description>New XML tutorial on W3School</description> </item> </channel> </rss>
下面分享一段使用 php 动态生成 RSS 的代码示例:
<?php /** ** php 动态生成 RSS 类 **/ define("TIME_ZONE",""); define("FEEDCREATOR_VERSION","www.111cn.net");//您的网址 class FeedItem extends HtmlDescribable{ var $title,$description,$link; var $author,$authorEmail,$image,$category,$comments,$guid,$source,$creator; var $date; var $additionalElements=Array(); } class FeedImage extends HtmlDescribable{ var $title,$url,$link; var $width,$height,$description; } class HtmlDescribable{ var $descriptionHtmlSyndicated; var $descriptionTruncSize; function getDescription(){ $descriptionField=new FeedHtmlField($this->description); $descriptionField->syndicateHtml=$this->descriptionHtmlSyndicated; $descriptionField->truncSize=$this->descriptionTruncSize; return $descriptionField->output(); } } class FeedHtmlField{ var $rawFieldContent; var $truncSize,$syndicateHtml; function FeedHtmlField($parFieldContent){ if($parFieldContent){ $this->rawFieldContent=$parFieldContent; } } function output(){ if(!$this->rawFieldContent){ $result=""; } elseif($this->syndicateHtml){ $result="<![CDATA[".$this->rawFieldContent."]]>"; }else{ if($this->truncSize and is_int($this->truncSize)){ $result=FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize); }else{ $result=htmlspecialchars($this->rawFieldContent); } } return $result; } } class UniversalFeedCreator extends FeedCreator{ var $_feed; function _setFormat($format){ switch (strtoupper($format)){ case "2.0": // fall through case "RSS2.0": $this->_feed=new RSSCreator20(); break; case "0.91": // fall through case "RSS0.91": $this->_feed=new RSSCreator091(); break; default: $this->_feed=new RSSCreator091(); break; } $vars=get_object_vars($this); foreach ($vars as $key => $value){ // prevent overwriting of properties "contentType","encoding"; do not copy "_feed" itself if(!in_array($key, array("_feed","contentType","encoding"))){ $this->_feed->{$key}=$this->{$key}; } } } function createFeed($format="RSS0.91"){ $this->_setFormat($format); return $this->_feed->createFeed(); } function saveFeed($format="RSS0.91",$filename="",$displayContents=true){ $this->_setFormat($format); $this->_feed->saveFeed($filename,$displayContents); } function useCached($format="RSS0.91",$filename="",$timeout=3600){ $this->_setFormat($format); $this->_feed->useCached($filename,$timeout); } } class FeedCreator extends HtmlDescribable{ var $title,$description,$link; var $syndicationURL,$image,$language,$copyright,$pubDate,$lastBuildDate,$editor,$editorEmail,$webmaster,$category,$docs,$ttl,$rating,$skipHours,$skipDays; var $xslStyleSheet=""; var $items=Array(); var $contentType="application/xml"; var $encoding="utf-8"; var $additionalElements=Array(); function addItem($item){ $this->items[]=$item; } function clearItem2Null(){ $this->items=array(); } function iTrunc($string,$length){ if(strlen($string)<=$length){ return $string; } $pos=strrpos($string,"."); if($pos>=$length-4){ $string=substr($string,0,$length-4); $pos=strrpos($string,"."); } if($pos>=$length*0.4){ return substr($string,0,$pos+1)." ..."; } $pos=strrpos($string," "); if($pos>=$length-4){ $string=substr($string,0,$length-4); $pos=strrpos($string," "); } if($pos>=$length*0.4){ return substr($string,0,$pos)." ..."; } return substr($string,0,$length-4)." ..."; } function _createGeneratorComment(){ return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n"; } function _createAdditionalElements($elements,$indentString=""){ $ae=""; if(is_array($elements)){ foreach($elements AS $key => $value){ $ae.= $indentString."<$key>$value</$key>\n"; } } return $ae; } function _createStylesheetReferences(){ $xml=""; if($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n"; if($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n"; return $xml; } function createFeed(){} function _generateFilename(){ $fileInfo=pathinfo($_SERVER["PHP_SELF"]); return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml"; } function _redirect($filename){ Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename)); Header("Content-Disposition: inline; filename=".basename($filename)); readfile($filename,"r"); die(); } function useCached($filename="",$timeout=3600){ $this->_timeout=$timeout; if($filename==""){ $filename=$this->_generateFilename(); } if(file_exists($filename) && (time()-filemtime($filename) < $timeout)){ $this->_redirect($filename); } } function saveFeed($filename="",$displayContents=true){ if($filename==""){ $filename=$this->_generateFilename(); } $feedFile=fopen($filename,"w+"); if($feedFile){ fputs($feedFile,$this->createFeed()); fclose($feedFile); if($displayContents){ $this->_redirect($filename); } }else{ echo "<br /><b>Error creating feed file, please check write permissions.</b><br />"; } } } class FeedDate{ var $unix; function FeedDate($dateString=""){ if($dateString=="") $dateString=date("r"); if(is_integer($dateString)){ $this->unix=$dateString; return; } if(preg_match("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)){ $months=Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12); $this->unix=mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]); if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){ $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60; }else{ if(strlen($matches[7])==1){ $oneHour=3600; $ord=ord($matches[7]); if($ord < ord("M")){ $tzOffset=(ord("A") - $ord - 1) * $oneHour; } elseif($ord >= ord("M") && $matches[7]!="Z"){ $tzOffset=($ord - ord("M")) * $oneHour; } elseif($matches[7]=="Z"){ $tzOffset=0; } } switch ($matches[7]){ case "UT": case "GMT": $tzOffset=0; } } $this->unix += $tzOffset; return; } if(preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)){ $this->unix=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]); if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){ $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60; }else{ if($matches[7]=="Z"){ $tzOffset=0; } } $this->unix += $tzOffset; return; } $this->unix=0; } function rfc822(){ $date=gmdate("Y-m-d H:i:s",$this->unix); if(TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE); return $date; } function iso8601(){ $date=gmdate("Y-m-d H:i:s",$this->unix); $date=substr($date,0,22) . ':' . substr($date,-2); if(TIME_ZONE!="") $date=str_replace("+00:00",TIME_ZONE,$date); return $date; } function unix(){ return $this->unix; } } class RSSCreator10 extends FeedCreator{ function createFeed(){ $feed="<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n"; $feed.= $this->_createGeneratorComment(); if($this->cssStyleSheet==""){ $cssStyleSheet="http://www.w3.org/2000/08/w3c-synd/style.css"; } $feed.= $this->_createStylesheetReferences(); $feed.= "<rdf:RDF\n"; $feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n"; $feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; $feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n"; $feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"; $feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n"; $feed.= " <title>".htmlspecialchars($this->title)."</title>\n"; $feed.= " <description>".htmlspecialchars($this->description)."</description>\n"; $feed.= " <link>".$this->link."</link>\n"; if($this->image!=null){ $feed.= " <image rdf:resource=\"".$this->image->url."\" />\n"; } $now=new FeedDate(); $feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n"; $feed.= " <items>\n"; $feed.= " <rdf:Seq>\n"; for ($i=0;$i<count($this->items);$i++){ $feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n"; } $feed.= " </rdf:Seq>\n"; $feed.= " </items>\n"; $feed.= " </channel>\n"; if($this->image!=null){ $feed.= " <image rdf:about=\"".$this->image->url."\">\n"; $feed.= " <title>".$this->image->title."</title>\n"; $feed.= " <link>".$this->image->link."</link>\n"; $feed.= " <url>".$this->image->url."</url>\n"; $feed.= " </image>\n"; } $feed.= $this->_createAdditionalElements($this->additionalElements," "); for ($i=0;$i<count($this->items);$i++){ $feed.= " <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n"; //$feed.= " <dc:type>Posting</dc:type>\n"; $feed.= " <dc:format>text/html</dc:format>\n"; if($this->items[$i]->date!=null){ $itemDate=new FeedDate($this->items[$i]->date); $feed.= " <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n"; } if($this->items[$i]->source!=""){ $feed.= " <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n"; } if($this->items[$i]->author!=""){ $feed.= " <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n"; } $feed.= " <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")))."</title>\n"; $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n"; $feed.= " <description>".htmlspecialchars($this->items[$i]->description)."</description>\n"; $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements," "); $feed.= " </item>\n"; } $feed.= "</rdf:RDF>\n"; return $feed; } } class RSSCreator091 extends FeedCreator{ var $RSSVersion; function RSSCreator091(){ $this->_setRSSVersion("0.91"); $this->contentType="application/rss+xml"; } function _setRSSVersion($version){ $this->RSSVersion=$version; } function createFeed(){ $feed="<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n"; $feed.= $this->_createGeneratorComment(); $feed.= $this->_createStylesheetReferences(); $feed.= "<rss version=\"".$this->RSSVersion."\">\n"; $feed.= " <channel>\n"; $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n"; $this->descriptionTruncSize=500; $feed.= " <description>".$this->getDescription()."</description>\n"; $feed.= " <link>".$this->link."</link>\n"; $now=new FeedDate(); $feed.= " <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n"; $feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n"; if($this->image!=null){ $feed.= " <image>\n"; $feed.= " <url>".$this->image->url."</url>\n"; $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n"; $feed.= " <link>".$this->image->link."</link>\n"; if($this->image->width!=""){ $feed.= " <width>".$this->image->width."</width>\n"; } if($this->image->height!=""){ $feed.= " <height>".$this->image->height."</height>\n"; } if($this->image->description!=""){ $feed.= " <description>".$this->image->getDescription()."</description>\n"; } $feed.= " </image>\n"; } if($this->language!=""){ $feed.= " <language>".$this->language."</language>\n"; } if($this->copyright!=""){ $feed.= " <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n"; } if($this->editor!=""){ $feed.= " <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n"; } if($this->webmaster!=""){ $feed.= " <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n"; } if($this->pubDate!=""){ $pubDate=new FeedDate($this->pubDate); $feed.= " <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n"; } if($this->category!=""){ $feed.= " <category>".htmlspecialchars($this->category)."</category>\n"; } if($this->docs!=""){ $feed.= " <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n"; } if($this->ttl!=""){ $feed.= " <ttl>".htmlspecialchars($this->ttl)."</ttl>\n"; } if($this->rating!=""){ $feed.= " <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n"; } if($this->skipHours!=""){ $feed.= " <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n"; } if($this->skipDays!=""){ $feed.= " <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n"; } $feed.= $this->_createAdditionalElements($this->additionalElements," "); for ($i=0;$i<count($this->items);$i++){ $feed.= " <item>\n"; $feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n"; $feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n"; $feed.= " <description>".$this->items[$i]->getDescription()."</description>\n"; if($this->items[$i]->author!=""){ $feed.= " <author>".htmlspecialchars($this->items[$i]->author)."</author>\n"; } /* // on hold if($this->items[$i]->source!=""){ $feed.= " <source>".htmlspecialchars($this->items[$i]->source)."</source>\n"; } */ if($this->items[$i]->category!=""){ $feed.= " <category>".htmlspecialchars($this->items[$i]->category)."</category>\n"; } if($this->items[$i]->comments!=""){ $feed.= " <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n"; } if($this->items[$i]->date!=""){ $itemDate=new FeedDate($this->items[$i]->date); $feed.= " <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n"; } if($this->items[$i]->guid!=""){ $feed.= " <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n"; } $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements," "); $feed.= " </item>\n"; } $feed.= " </channel>\n"; $feed.= "</rss>\n"; return $feed; } } class RSSCreator20 extends RSSCreator091{ function RSSCreator20(){ parent::_setRSSVersion("2.0"); } }
使用示例:
<?php header('Content-Type:text/html; charset=utf-8'); $db=mysql_connect('127.0.0.1','root','123456'); mysql_query("set names utf8"); mysql_select_db('dbname',$db); $brs=mysql_query('select * from article order by add_time desc limit 0,10',$db); $rss=new UniversalFeedCreator(); $rss->title="页面标题"; $rss->link="网址http://"; $rss->description="rss标题"; while($rowbrs=mysql_fetch_array($brs)){ $item=new FeedItem(); $item->title =$rowbrs['subject']; $item->link='http://www.111cn.net/'; $item->description =$rowbrs['description']; $rss->addItem($item); } mysql_close($db); $rss->saveFeed("RSS2.0","rss.xml");
相关文章
- 由于要使用mikoomi mongodb plugin插件,所以需要php对mongodb的扩展支持,默认通过源安装的php并没有mongodb的扩展支持,具体可以通过php -m|grep mongo 验证 。这里就结...2016-11-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 安装curl扩展支持https是非常的重要现在许多的网站都使用了https了,下面我们来看一篇关于PHP安装curl扩展支持https例子吧。 问题: 线上运行的lamp服务器,默认yu...2016-11-25
- 这篇文章主要介绍了IDEA中的clean,清除项目缓存图文教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
PHP分布式框架如何使用Memcache同步SESSION教程
本教程主要讲解PHP项目如何用实现memcache分布式,配置使用memcache存储session数据,以及memcache的SESSION数据如何同步。 至于Memcache的安装配置,我们就不讲了,以前...2016-11-25- PHP是一种解释型的语言,对于用户而言,我们精心的控制内存意味着easier prototyping和更少的崩溃!当我们深入到内核之后,所有的安全防线都已经被越过,最终还是要依赖于真正有责任心的软件工程师来保证系统的稳定运行。1、线...2015-11-08
- 这篇文章主要介绍了浅谈Vue开发人员的7个最好的VSCode扩展,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
- 这篇文章主要给大家介绍了关于iOS蓝牙设备名称缓存问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
- 这篇文章主要介绍了AngularJS实现Model缓存的方式,分享了多种AngularJS实现Model缓存的方法,感兴趣的小伙伴们可以参考一下...2016-02-05
- 本文给大家一起探讨nodejs下dns的缓存问题,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧...2016-11-22
- 这篇文章主要介绍了@CacheEvict + redis实现批量删除缓存方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-12
- 一、下载pthreads扩展下载地址:http://windows.php.net/downloads/pecl/releases/pthreads二、判断PHP是ts还是nts版通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说...2015-11-24
- memche消息队列的原理就是在key上做文章,用以做一个连续的数字加上前缀记录序列化以后消息或者日志。然后通过定时程序将内容落地到文件或者数据库。php实现消息队列的用处比如在做发送邮件时发送大量邮件很费时间的问...2014-05-31
- 在本篇文章里小编给大家整理的是一篇关于python删除缓存文件方法,需要的朋友们可以学习下。...2020-07-19
- 这篇文章主要介绍了IIS7、iis7.5中禁止缓存单个静态文件的配置方法,需要的朋友可以参考下...2017-07-06
- 这篇文章主要介绍了vue项目中禁用浏览器缓存配置案例,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-09-12
- 这篇文章主要介绍了Redis集群水平扩展、集群中添加以及删除节点的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-25
- 页面不缓存可以让我们有更新就立即更新出来用户不需要清除浏览器缓存或不停的按f5刷新了,这里整理了解一些关于页面不缓存的方法,具体的如下。 一,js,css,图片文件不...2016-09-20
- 小编给大家带来一篇关于安卓手机缓存怎么清理的问题解答,有需要的可以参考一下   安卓手机怎么清理缓存 android清除程序缓存的方法  一,...2017-07-06
SpringCache 分布式缓存的实现方法(规避redis解锁的问题)
这篇文章主要介绍了SpringCache 分布式缓存的实现方法(规避redis解锁的问题),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-20