Use PHP to display any RSS feed on your siteOver the years, I've struggled with ways to include external content on this site, without having to use JavaScript or Flash "widgets," which are convenient but can be troublesome. I even got philosophical and preached that external content should remain external - relative to it's domain. Despite all this, occasionally I'll want to display some content from another site, such as my Twitter updates, or my Google Reader Shared links. With PHP 5, it's very easy to include external content onto your site. This tutorial will explain how to display the contents of an RSS feed directly into your site's HTML markup. The benefits of parsing external content are:
RSS feedFirst, find the RSS feed URL for the content you'd like displayed on your site. For this example, we'll use my latest Twitter updates RSS feed:
Below is an example "node" of this RSS feed: <item> <title>Matthom: I've come to the conclusion there is no one-stop-shop browser.</title> <description>Matthom: I've come to the conclusion there is no one-stop-shop browser.</description> <pubDate>Tue, 18 Sep 2007 14:53:35 +0000</pubDate> <guid>http://twitter.com/Matthom/statuses/276870562</guid> <link>http://twitter.com/Matthom/statuses/276870562</link> </item> PHPWith PHP, we can parse this information, and display it in a format more comfortable to us, such as a list item:
Using basic cURL syntax, we'll loop through each
# INSTANTIATE CURL.
$curl = curl_init();
# CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/122933.rss");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
# GRAB THE XML FILE.
$xmlTwitter = curl_exec($curl);
curl_close($curl);
# SET UP XML OBJECT.
$xmlObjTwitter = simplexml_load_string( $xmlTwitter );
$tempCounter = 0;
foreach ( $xmlObjTwitter -> item as $item )
{
# DISPLAY ONLY 10 ITEMS.
if ( $tempCounter < 11 )
{
echo "<li><a href=\"{$item -> guid}\">{$item -> title}</a></li>
";
}
$tempCounter += 1;
}
ResultThis code will loop through each RSS feed
<ul>
<li>Matthom: I've come to the conclusion there is no one-stop-shop browser.</li>
<li>Matthom: At the iTunes store.</li>
<li>Matthom: @MattsShell - We should go to Jewel and pick some up.</li>
<li>Matthom: Collective Soul - Georgia Girl</li>
<li>Matthom: woo chilly!</li>
<li>Matthom: Suppose to be 85 so am wearing shorts again</li>
<li>Matthom: heading out the door.</li>
<li>Matthom: I need to start buying some frozen lunch meals</li>
<li>Matthom: Is it only Tuesday?</li>
<li>Matthom: Going to eat. Tacos!</li>
</ul>
Nice, clean, and looks like it comes straight from my own database. Other resources
Comments/Mentions
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|
Hi, I'm trying your method within a TextPattern istallation - therefore between ~ and getting the following error:
Parse error: syntax error, unexpected '<' in /users/home/davidhall/web/public/textpattern/publish/taghandlers.php(2745) : eval()'d code on line 2
Test page is: http://david-hall.info/trial
Probably something I'm doing wrong, but any ideas?
Thanks, David