0

我正在尝试使用 SimplePie 为客户解析 RSS 提要(客户是华盛顿邮报的作者)。

通读文档并使用示例代码作为参考后,我能够将提要解析到站点中,但现在我遇到了撇号字符未解码的问题('显示为')

我尝试使用 SimplePie 常见问题解答中建议的解决方案来解决此问题: 1. 验证网站的元标记 2. 使用 SimplePie 的 handle_content_type() 函数 3. 使用 PHP 的内置 header() 函数来更正 HTTP 标头

不幸的是,这些都没有为我解决问题。

下面是我用来解析 RSS 提要的代码:

<?php

require_once('php/autoloader.php');

$feedJB = new SimplePie();
$feedJB->set_feed_url('http://washingtontimes.dynamic.feedsportal.com/pf/637323/communities.washingtontimes.com/neighborhood/feeds/latest/status-update/');
$feedJB->init();
$feedJB->handle_content_type();

$feedRB = new SimplePie();
$feedRB->set_feed_url('http://washingtontimes.dynamic.feedsportal.com/pf/637323/communities.washingtontimes.com/neighborhood/feeds/latest/2nd-golden-era-advertising/');
$feedRB->init();
$feedRB->handle_content_type();

?>

这是页面上的输出代码:

<!-- Left -->
            <li class="left">
                <h3>Recent Posts</h3>
                <ul class="feed-list">
                    <?php foreach ($feedJB->get_items(0, 5) as $item): ?>
                    <li>
                        <strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong>
                        <small>Posted on <?php echo $item->get_date('j F Y'); ?></small>
                    </li>
                    <?php endforeach; ?>
                    <li><h4><a href="<?php echo $feedJB->get_permalink(); ?>">Read more articles by Jeff</a></h4></li>
                </ul>
            </li>
            <!-- /Left -->

            <!-- Right -->
            <li class="right">
                <h3>Recent Posts</h3>
                <ul class="feed-list">
                    <?php foreach ($feedRB->get_items(0, 5) as $item): ?>
                    <li>
                        <strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong>
                        <small>Posted on <?php echo $item->get_date('j F Y'); ?></small>
                    </li>
                    <?php endforeach; ?>
                    <li><h4><a href="<?php echo $feedRB->get_permalink(); ?>">Read more articles by Rob</a></h4></li>
                </ul>
            </li>
            <!-- /Right -->

我已经在我的机器(运行 MAMP 的 Mac Pro Lion)以及我的 Web 服务器(运行 Apache 2.2.22 和 PHP 5.2.17 的 Linux)上进行了本地测试。

您也可以通过以下链接暂时查看此内容: http ://clients.josephmainwaring.com/statuscreative/#!columns.php

如果有人有解决字符编码问题的建议,将不胜感激。

4

1 回答 1

0

I've found that the Washington Post's feeds are all served as ISO-8859-1 even when they contain UTF-8 characters. I don't use SimplePie, but every time I fetch a feed, I run it through the following function, where $xml is the text of the feed, and $url is the feed's URL:

function feed_fix_broken ( $xml, $url ) {
  $xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml );
  $broken = array ('washingtonpost.com' => 'ISO-8859-1');
  foreach ($broken as $domain => $encoding) {
    if (stristr($url, $domain)) {
      $xml = iconv( 'UTF-8', $encoding.'//TRANSLIT', $xml );
    }
  }
  return $xml;
}

This transliterates UTF-8 encoded entities to their ISO-8859-1 counterparts, where possible.

Notice that in FeedDemon, "Chávez" is screwy...

but I've got it right.

but I've got it right

于 2012-08-13T17:57:38.357 回答