Apr 08

Hello fellas,  because these days i had a case where i had to include some RSS feed on my site and i needed a very simple and fast solution in php how to do it i was very dissapointed from what i’ve found on the web. There are all kind of widgets, tools PHP scripts using the xml-reader class, whatever – but i just needed some very simple solution. So ofcourse i had to write it myself at the end :)

in this example i’m going to import feed from yahoo’s technology news rss:

first i’m going to use the “simplexml_load_file” to check what we got in this yahoo url

<?php
$xmlobj = simplexml_load_file(“http://rss.news.yahoo.com/rss/tech”);
print header(“Content-type: text/plain”);
print_r($xmlobj);
?>

my output on the browser looks like this

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Yahoo! News: Technology News
            [copyright] => Copyright (c) 2009 Yahoo! Inc. All rights reserved.
            [link] => http://news.yahoo.com/i/738
            [category] => technology
            [description] => Technology News
            [language] => en-us
            [lastBuildDate] => Wed, 08 Apr 2009 12:02:30 GMT
            [ttl] => 5
            [image] => SimpleXMLElement Object
                (
                    [title] => Yahoo! News
                    [width] => 142
                    [height] => 18
                    [link] => http://news.yahoo.com/i/738
                    [url] => http://l.yimg.com/a/i/us/nws/th/main_142b.gif
                )

            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Cable's answer to online's ad success: targeting
    (AP)

                            [link] => http://us.rd.yahoo.com/dailynews/rss/tech/*http://news.yahoo.com/s/ap/20090407/ap_on_hi_te/cable_targeted_advertising

                            [category] => technology
                            [pubDate] => Tue, 07 Apr 2009 20:37:20 GMT
                            [description] => <p><a href="http://us.rd.yahoo.com/dailynews/rss/tech/*http://news.yahoo.com/s/ap/20090407/ap_on_hi_te/cable_targeted_advertising"><img src="http://d.yimg.com/a/p/ap/20090407/capt.0d1190bc652444d0b0f4eb9bb9f0836c.cable_targeted_advertising_nybz175.jpg?x=130&y=86&q=85&sig=52xM5q9qwuq.Eq2_2pqO3A--" align="left" height="86" width="130" alt="FILE - In this April 2, 2009 file photo, a view of the user interface for Tru2way technology is seen on display inside the Broadband Nation exhibit at The Cable Show at the Washington Convention Center in Washington. Cable's efforts to standardize their systems around Tru2way will make it easier to deploy so-called 'addressable' ads regardless of operator, said Dallas Clement, senior vice president of strategy and development at Cox Communications. (AP Photo/Gerald Herbert, file)" border="0" /></a>AP - You're watching Jon Stewart's "The Daily Show," when suddenly you see a commercial for the Mustang convertible you've been eyeing — with a special promotion from Ford, which knows you just ended your car lease.</p><br clear="all"/>
                        )

so i won't be needing the RSS's channel news, i want only the items in that -> so i do:
<?php
$xmlobj = simplexml_load_file("http://rss.news.yahoo.com/rss/tech");
print header("Content-type: text/plain");
print_r($xmlobj->channel->item);
?>

after that i can remove the "print header("Content-type: text/plain");" ( i used it just not to show the images but to be able to see all the site just like a raw text :)  )

so all the code that i'm going to need for my RSS Feed importer will be :

<?php
$xmlobj = simplexml_load_file("http://rss.news.yahoo.com/rss/tech");

foreach($xmlobj->channel->item as $item){
	echo "<a href='".$item->link."'>".$item->title."</a><br />";
	echo $item->description."<br />";
}
?>
and here is the result:

including rss from yahoo feeder using php

so 4 rows in php and voilla( i hope that's the way to spell it :) )
we gotta fully working RSS(xml) importer for our shiny site -)

One Response to “Simple script to feed your site with some RSS that you like ;)”

  1. Christian Louboutin outlet Says:

    He might for instance deduce that someone with tapered fingers any person who transferred them regularly and deftly was a pianist, or that someone with grubby nails

Leave a Reply

*

Spam Protection by WP-SpamFree