【问题标题】:Bundle RSS in emails在电子邮件中捆绑 RSS
【发布时间】:2012-02-24 05:02:54
【问题描述】:

我一直在使用以下代码来获取 Google 新闻,但是,我需要将网站上的最终结果设为实际的 rss 提要,以便其他人可以获取该提要。现在输出创建了一个漂亮的 index.php 页面。这很好,但不符合我的目的。 SimplePie 可以创建格式为 rss 输出的页面以供抓取吗?

提前谢谢你。


<?php

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc.
require_once('simplepie.inc');

// We'll process this feed with all of the default options.
$feed = new SimplePie();

// Set the feed to process.
$feed->set_feed_url('http://news.google.com/news?hl=en&gl=us&q=new+york+commercial+real+estate&ie=UTF-8&output=rss');

// Run SimplePie.
$feed->init();

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code.  The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Sample SimplePie Page</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

</head>
<body>

        <div class="header">
        <h2><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h2>
        <p><?php echo $feed->get_description(); ?></p>
    </div>

    <?php
    /*
    Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
    */
    foreach ($feed->get_items() as $item):
    ?>

        <div class="item">
            <h4><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4>
            <p><?php echo $item->get_description(); ?></p>
            <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
        </div>

    <?php endforeach; ?>

</body>
</html>

【问题讨论】:

  • 仅供参考,我认为以您描述的方式重新包装 Google 新闻违反了他们的服务条款。

标签: php rss simplepie


【解决方案1】:

SimplePie 仅用于解析阶段,需要自己编写代码输出为 RSS。

你可以用快速而肮脏的方式来做:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
    <channel>
        <title>Your Site</title>

<?php
        foreach ($feed->get_items() as $item):
?>
        <entry>
            <title><?php echo $item->get_title() ?></title>
            <description><?php echo $item->get_content() ?></description>
            <!-- ... -->
        </entry>
    </channel>
</rss>

更好的方法是使用 SimpleXML 创建元素,然后输出结果。这将确保您的 XML 格式正确,并且所有内容都正确转义。

但是,正如 Brad 所说,您在执行此操作时还必须注意服务条款。你至少应该确保正确的归属。

【讨论】:

  • 谢谢,我非常担心违反任何服务条款。我确实阅读了他们的服务条款,如果您确保正确归属,这将不是问题,但我当然愿意就此进行讨论,因为它似乎有点不清楚。至于代码sn-ps,谢谢建议,我试试看。
  • 关于 Ryan 提供的快速简单的代码——是集成到 SimplePie 中的代码还是完全独立于 SimplePie 的独立代码?我看到 $item->get_title() 看起来像 simplepie 库代码,并且找不到对提要 URL 的引用。谢谢。
  • 该代码可以放在您想要输出提要的任何位置。在上面的示例中,它替换了“让我们开始我们的 XHTML 网页代码”之后的所有内容。这也只是一个非常简单的示例,完整的提要将包含更多信息,但这只是将标签与 SimplePie 方法匹配的情况。
猜你喜欢
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2019-03-19
  • 2018-05-30
  • 2017-06-03
相关资源
最近更新 更多