Smile 笑容

MAGPIERSS 的 Cookbook

MAGPIERSS 的中文文档比较少。在使用过程中把简单的Cookbook翻译了一下,其实很多人这个不翻译都可以看懂。但是很多人似乎不那么喜欢看文档,而喜欢去看例子;学看文档吧,可以解决很多问题。 如果你在 MAGPIERSS 的使用中,遇到错误,应该第一个检查的是你的RSS源 是否正确。

@encode utf8

MAGPIERSS RECIPES:  Cooking with Corbies

         "Four and twenty blackbirds baked in a pie."

1. LIMIT THE NUMBER OF HEADLINES(AKA ITEMS) RETURNED.
限制返回条目数

PROBLEM:
问题

You want to display the 10 (or 3) most recent headlines, but the RSS feed
contains 15.
如果你打算显示10条或3条 内容,但是 RSS 的 Feed 数据返回是15条

SOLUTION:
解决方案

$num_items = 10;
$rss = fetch_rss($url);

$items = array_slice($rss->items, 0, $num_items);

DISCUSSION:
讨论/更进一步的解决方法

Rather then trying to limit the number of items Magpie parses, a much simpler,
and more flexible approach is to take a "slice" of the array of items.  And
array_slice() is smart enough to do the right thing if the feed has less items
then $num_items.
有一个方法比使用 限定 $num_items  更好,就是我们可以使用数组切片函数(php内置函数)
array_slice()  可以更方便的实现这个功能,详细内容可以察看php手册:

See: http://www.php.net/array_slice


2. DISPLAY A CUSTOM ERROR MESSAGE IF SOMETHING GOES WRONG
显示定制的错误信息

PROBLEM:
问题

You don't want Magpie's error messages showing up if something goes wrong.
不希望 Magpie 在发生错误的时候显示出来,希望通过自己的方式来处理

SOLUTION:
解决方案

# Magpie throws USER_WARNINGS only
# so you can cloak these, by only showing ERRORs
# 关闭错误显示
error_reporting(E_ERROR);

# check the return value of fetch_rss()
# 获取返回值
$rss = fetch_rss($url);

if ( $rss ) {
...display rss feed...
正常处理
}
else {
//错误信息
 echo "An error occured!  " .
      "Consider donating more $$$ for restoration of services." .
   "<br>Error Message: " . magpie_error();
}

DISCUSSION:
讨论/更进一步的解决方法

MagpieRSS triggers a warning in a number of circumstances.  The 2 most common
circumstances are:  if the specified RSS file isn't properly formed (usually
because it includes illegal HTML), or if Magpie can't download the remote RSS
file, and there is no cached version. 
通常有两种情况下会触发错误,一个是获取的内容格式不对,另外一个是
不能连接远程主机且没有本地缓存版本。

If you don't want your users to see these warnings change your error_reporting
settings to only display ERRORs.  Another option is to turn off display_error,
so that WARNINGs, and NOTICEs still go to the error_log but not to the webpages.
若不希望或不需要显示错误信息,那么可以直接关闭错误信息。

You can do this with:
关闭错误信息显示的方法:

ini_set('display_errors', 0);

See: http://www.php.net/error_reporting,
     http://www.php.net/ini_set,
  http://www.php.net/manual/en/ref.errorfunc.php

3. GENERATE A NEW RSS FEED
生成一个新的RSS Feed

PROBLEM:
问题

Create an RSS feed for other people to use.
创建RSS Feed 供其他人使用

SOLUTION:
解决方案

Use Useful Inc's RSSWriter (http://usefulinc.com/rss/rsswriter/)
推荐使用 RSSWriter

DISCUSSION:
讨论/更进一步的解决方法

An example of turning a Magpie parsed RSS object back into an RSS file is forth
coming.  In the meantime RSSWriter has great documentation.
即将推出一个通过 Magpie parsed RSS 对象 重新写回 RSS 文件的例子。
RSSWriter 拥有一份很棒的文档资料。

4. DISPLAY HEADLINES MORE RECENT THEN X DATE
显示某个日期之前的历史

PROBLEM:
问题

You only want to display headlines that were published on, or after a certain
date.
希望显示某个确定日期前的历史条目


SOLUTION:
解决方案

require 'rss_utils.inc';

# get all headlines published today
$today = getdate();

# today, 12AM
$date = mktime(0,0,0,$today['mon'], $today['mday'], $today['year']);

$rss = fetch_rss($url);

foreach ( $rss->items as $item ) {
 $published = parse_w3cdtf($item['dc']['date']);
 if ( $published >= $date ) {
  echo "Title: " . $item['title'];
  echo "Published: " . date("h:i:s A", $published);
  echo "<p>";
 }
}

DISCUSSION:
讨论/更进一步的解决方法

This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
(which is very good RSS style)
这个方法只能运行于RSS1.0的Feeds上,因为它包含了 data 字段。
(是一个非常棒的RSS版本)

parse_w3cdtf is defined in rss_utils.inc, and parses RSS style dates into Unix
epoch seconds. 
parse_w3cdtf 函数是在 rss_utils.inc 中定义的,时间是格式是 Unix 的时间戳。

See: http://www.php.net/manual/en/ref.datetime.php