-
Notifications
You must be signed in to change notification settings - Fork 25
API: PHP: Feed
Feed API enables easy parsing of RSS feeds.
To start off, a feed object should be created with a unique feed name, used to distinguish it in a feed set, and also an RSS URL, that is a direct path to a well formatted feed.
$feed = new Feed({FEED_NAME}, {FEED_URL});
Once created, the feed object will not request or parse any data until feed items are requested. To get all available feed objects, use the following syntax:
$feed_items = $feed->get_items();
Each feed item contains information such as feed title, description, author, publish date, link, and title. To get a short description, use $feed_item->get_short_description()
. This method can also take in an optional parameter that determines the minimum number of characters to be included in the description.
With the code above, we can create a menu listing the feed item's title and some part of its description.
<div class="menu-full menu-detailed menu-padded">
<h1 class="menu-first">RSS Feed Title Menu</h1>
<ol>
<?PHP
$feed = new Feed("{FEED_NAME}", "{FEED_URL}");
$feed_items = $feed->get_items();
foreach($feed_items as $feed_item)
{
echo "<li>";
echo "<a href='".$feed_item->get_page()."'>";
echo $feed_item->get_title()."<br>";
echo "<span class='smallprint'>".$feed_item->get_short_description()."</span>";
echo "</a>";
echo "</li>";
}
?>
</ol>
</div>
The above code along with some styling produces a display that has the following format in iPhone:
For easily creating URLs that point to the feed article, each item provides a method $feed_item->get_page()
, that combines the feed name, feed path, and the article title in a URL encoded path string. The created URL has the following format - 'item.php?name=feed_name&path=feed_path&article=article_title'.
Optionally, if a salt value is provided to the get_page()
, then the URL will have an extra parameter signature
that will be the MD5 hash of all the mentioned values combined with the provided salt value. To check for authenticity, $feed_item->verify_page($signature, $salt)
method should be utilized with the same salt value.
Given that the user was redirected to the item view via the constructed get_page()
, then the item should be displayed in item.php
, optionally utilizing the provided helper methods. The code will be as follows:
$salt = ...;
//Uses the set GET parameters to construct a feed and a feed item object.
$feed = Feed::build_page_from_request();
$feed_item = $feed->build_item_from_request();
//Optionally, check the signature and verify the page.
if(isset($_GET["signature"]) && !$feed_item->verify_page($_GET['signature'], $salt))
{
die();
}
And the MWF code to view the item would be something similar to below code:
<div class="content-full content-padded">
<h1 class="content-first light align-left"><?PHP echo $feed_item->get_title(); ?></h1>
<p>
<?PHP echo $feed_item->get_short_description(); ?>
</p>
<p class="content-last">
-Article Written by <?PHP echo $feed_item->get_author();?>
</p>
</div>
The above code will produce an output such as this: