[转]MagpieRSS解析程序及乱码解决

[url]http://www.niutian365.com/blog/article.asp?id=153[/url]

最近在网上转,发现了一个用PHP写的解析RSS的好工具——MagpieRSS,它支持绝大部分的RSS调用,而且支持UTF-8编码,不过最近有些人说是用的时候出现乱码,下面我就给大家简单的说一下。可以说,MagepieRSS的使用是很简单的,在程序的README和INSTALL里面都有详细的使用说明。具体代码如下:

require_once(rss_fetch.inc);
$url = $_GET['url'];
$rss = fetch_rss( $url );

echo "Channel Title: " . $rss->channel['title'] . "<p>";
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
echo "<li><a href=$href>$title</a></li>";
}
echo "</ul>";

可能有些人在打开中文的调用时是空白的,什么都不显示,这主要是编码的问题,你可以在上面的程序的最上面加入下面的一句:

define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');//UTF-8格式

但是这样加了之后,很多人会看到乱码,这很可能是你的浏览器的默认编码不对,你可以手动选择UTF-8编码就可以了,你也可以在显示这些内容的页面定义显示的编码:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

这样的话,只要你的IE浏览器编码设置为自动选择的时候就可以正常显示了。
而很多时候我们想用GB2312,那我们可以用PHP里面的一个转化函数(mb_convert_encoding或iconv)来实现UTF-8到GB2312的转化。下面是mb_convert_encoding的实例:

mb_convert_encoding( $rss->channel['title'], 'GB2312', 'UTF-8' );

不过在用mb_convert_encoding()函数的时候,别忘了在php.ini里面把extension=php_mbstring.dll前面的分号去掉,把mbstring扩展加近来。

MagpieRSS是一个很好的开源的程序,对于我们这些经常制作网站的人来说,是相当好用的一个工具,希望大家喜欢。
来源:http://blog.fesite.com/read.php?24

MagpieRSS vs LastRSS : comparison of PHP RSS parsers
http://frenchfragfactory.net/ozh/archives/2004/11/10/magpierss-vs-lastrss-comparison-of-php-rss-parsers/


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

原文来自于 八小时外: http://oo8h.com/news_589.html
@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." .
"
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 "
";
}
}

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

This recipe only works for RSS 1.0 feeds that include the 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 的时间戳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值