红人网事 |
Posted: 23 Mar 2009 03:59 AM PDT |
Posted: 22 Mar 2009 08:20 PM PDT 3月15号发了一篇文章《通过RSS将wordpress的最新文章调用到其他网站》,一位朋友问我,RSS远程调用的时候如何取得feed源站点的文章全文,又如何控制feed的输出数量。关于这两个问题我做一下解答。 第一,全文输出得看feed源了,若对方提供全文输出,则用一段php代码是可以实现远程的RSS全文显示的。 代码如下: <?php $RSSURL = "http://feeds2.feedburner.com/redlogpress"; $buff = ""; $fp = fopen($RSSURL,"r"); while ( !feof($fp) ) { $buff .= fgets($fp,4096); } fclose($fp); $parser = xml_parser_create(); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_parse_into_struct($parser,$buff,$values,$idx); xml_parser_free($parser); $channel_title = $values[2]["value"]; echo <<<__HTML__ <html> <head> <meta http-equiv='content-type' content='text/html; charset=UTF-8'> <title>$channel_title</title> <link rel='stylesheet' type='text/css' id='css' href='http://www.redren.com/themes/reddigi2/style.css'> </head> <body> <table border='0'> <tr> <td> <img src='http://www.redren.com/themes/reddigi2/images/logo.jpg'> </td> <td> $channel_title $channel_lastBuildDate<br> </td> </tr> __HTML__; $in_item = 0; foreach ($values as $value) { $tag = $value["tag"]; $type = $value["type"]; $value = $value["value"]; $tag = strtolower($tag); if ($tag == "item" && $type == "open") { $in_item = 1; } else if ($tag == "item" && $type == "close") { echo <<<EOM <tr> <td colspan='2' class='header'width='400'> <a href="$link">$title</a> </td> </tr> <tr> <td colspan='2' width='400'align='right'> $pubDate </td> </tr> <tr> <td colspan='2' width='400'> $description </td> </tr> <tr> <td> </td> </tr> EOM; $in_item = 0; } if ($in_item) { switch ($tag) { case "title": $title = $value; break; case "link": $link = $value; break; case "pubDate": $pubDate = $value; break; case "description": $description = $value; break; } } } echo <<< __HTMLEND__ </table> </body> </html> __HTMLEND__; ?> 这段代码靠一个css文件控制其输出格式,具体请仔细参阅代码。 第二,控制feed的输出条数,我提供另一种RSS读取的php代码,本例将RSS读取类先给封装一次,然后用传统的模板格式将其显示出来。 封装的RSS类,文件名设定为 rss.class.php,代码如下: <?php class ReadRSS{ var $url; var $content; var $values; function ReadRSS($url){ $this->url=$url; } function ReadFile(){ $fp = fopen($this->url,"r"); while ( !feof($fp) ) { $this->content .= fgets($fp,4096); } fclose($fp); } function ReadXML(){ $parser = xml_parser_create();//建立xml解析器 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);//忽略由空白字符组成的值 xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,'UTF-8'); //设置编码,目前只支持ISO-8859-1、US-ASCII 和 UTF-8,也可以不设置,与目标编码一致 xml_parse_into_struct($parser,$this->content,$this->values); xml_parser_free($parser); } function RSS($n=10){ $this->ReadFile(); $this->ReadXML(); $in_item = 0; $i=0; $read=array(); foreach ($this->values as $value) { $tag = $value["tag"]; $type = $value["type"]; $value = $value["value"]; $tag = strtolower($tag); if ($tag == "item" && $type == "open") { $in_item = 1; } else if ($tag == "item" && $type == "close"){ $read[$i]['link']=$link; $read[$i]['title']=$title; if ($pubDate) { $read[$i]['pubDate']=$pubDate; } $read[$i]['description']=$description; $in_item = 0; $i++; if ($i>=$n) { break; } } if ($in_item) { switch ($tag) { case "title": $title = $value; break; case "link": $link = $value; break; case "pubDate": $pubDate = $value; break; case "description": $description = $value; break; } } } return $read; } } ?> 模板代码如下: <html> <head> <meta http-equiv='content-type' content='text/html; charset=UTF-8'> <title>红人网事</title> </head> <body> <?php include_once('rssclass/rss.class.php'); $url="http://blog.redren.com/feed/"; $rss=new ReadRSS($url); $values=$rss->RSS(10); # 10 可以改成别的数字,比如5,这是读取feed的条数 foreach ($values as $value) { echo "<div><a href={$value['link']}>{$value['title']}</a></div>"; } ?> </body> </html> 这个模板调用了刚才建立的位于 rssclass 这个文件夹下的 rss.class.php RSS类。 标签:rss, wordpress您或许对以下几篇文章也有兴趣: |
You are subscribed to email updates from 红人网事 To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Inbox too full? Subscribe to the feed version of 红人网事 in a feed reader. | |
If you prefer to unsubscribe via postal mail, write to: 红人网事, c/o Google, 20 W Kinzie, Chicago IL USA 60610 |
没有评论:
发表评论