1

I've been using the following code to pull in Google news, however, I need to have the final result on the web site be an actual rss feed so others can grab the feed. Right now the output creates a nice index.php page. That's great, but doesn't suit my purposes. Can SimplePie create a page which is formatted as an rss output for grabbing purposes?

thank you in advance.


<?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>
Ryan McCue
  • 1,628
  • 14
  • 23
Lester Kahn
  • 31
  • 1
  • 1
  • 4
  • FYI, I believe repackaging Google News in the way that you are describing violates their Terms of Service. – Brad Feb 24 '12 at 05:05

1 Answers1

0

SimplePie is used only for the parsing stage, you need to write your own code to output it as RSS.

You can do it the quick and dirty way:

<?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>

A much better way is to use SimpleXML to create the elements and then output the result of that. That will ensure that your XML is well-formed, and that everything is escaped correctly.

However, you also have to be careful about terms of service when doing this, as Brad mentioned. You should ensure proper attribution at the very least.

Ryan McCue
  • 1,628
  • 14
  • 23
  • Thanks and I am very concerned about violating any terms of service. I did read their terms of service and it seems if you ensure proper attribution, it would not be an issue, but I am certainly open to discussion on this as it seems kind of unclear. As for the code snippets, thanks for the suggestions, I will give it a try. – Lester Kahn Feb 24 '12 at 13:55
  • Regarding the quick and simple provided by Ryan -- is that code that would be integrated into SimplePie or independent code wholly separate from SimplePie? I see the $item->get_title() looks like simplepie library code and can find no reference to a feed URL. Thank you. – Lester Kahn Feb 24 '12 at 14:15
  • That code goes wherever you want to output the feed. In the example above, it replaces everything after "Let's begin our XHTML webpage code." It is also only a very quick example, a full feed would include much more information, however that's simply a case of matching tags up with SimplePie methods. – Ryan McCue Feb 25 '12 at 13:07