Archive for November, 2010

RSS Reader – PHP Script For Displaying An RSS Feed On A Web Page

Display RSS Feeds On Your Website

Here I am going to go over a little plugin that will allow you to display RSS Feeds on to your site

I was searching the web for an easy feed puller and was having no luck comping up with one that let me have full control of the style well keeping things simple for me to install and use, so I had to create one. But lucky for me a lot of the code was the same as the Twitter Feeds I made a week or so ago that you can find HERE.

In the download you will see two .php files 1:(RSS_Feeds.php) 2:(RSS_Feeds_striped.php). RSS_Feeds.php is the file that I used in the demo so it has the style and a few other divs in it well RSS_Feeds_striped.php is just the php code that you need to get this thing working.

OK, lets get to the demos! :)

Demo 1: Live Demo Here
This is a demo of me adding a little design to the feeds.

Demo 2: Live Demo Here
This is a demo with out any style at all.

Now that we have the Demos out of the way lets get to the stuff you really want!! The download so you can have a go at the files and see if it works for you.
DOWNLOAD HERE!!

As a note I have only tested this with three (3) types of RSS feeds so if you find one that this does not work on let me know and I will try my best to make it work! :) Also if you have any problems or see anything that could be done better let me know and I will take another look at the code.

The three feeds I have tested this on are NPR , Yahoo News and WordPress (my blog – the one you are reading).

So I guess we should get in to some of the code? Ok, fine I will talk for a little bit about how to set this script up.

To get started there are really only three lines you will need to change and that is these three lines.

  1.  
  2. curl_close($feed_curl);
  3. $feed = $RSS_Feed_URL;
  4. $feed_curl = curl_init();
  5. $timeout = 5;                                                                                           // set to zero for no timeout
  6. curl_setopt ($feed_curl, CURLOPT_URL, $feed );
  7. curl_setopt ($feed_curl, CURLOPT_RETURNTRANSFER, 1);
  8. curl_setopt ($feed_curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  9. $file_contents = curl_exec($feed_curl);
  10. $feed_content = $file_contents;
  11. $the_feed = explode("<item>", $feed_content);
  12. $tcount = count($the_feed) – 1;
  13. for ($i = 1; $i <= $tcount; $i++) {
  14. $end_feed = explode("</item>", $the_feed[$i]);
  15.  

This next two lines tells are calling in the description witch I am calling content.

  1.  
  2. $description = explode("<description>", $end_feed[0]);
  3. $content = explode("</description>", $description[1]);
  4.  

The next three lines were added to fix a few problems I ran in to.

  1.  
  2. $content[0] = str_replace("<![CDATA[", " ", $content[0]);       // Needed for Word press
  3. $content[0] = str_replace("]]>", " ", $content[0]);             // Needed for Word press
  4. $content[0] = str_replace("&#60;", "<", $content[0]);           // This makes images and links work
  5.  

The WordPress fix was added becouse I was running in to a problem with WordPress putting all the content in a CDATA call and not allowing the content to show in my page. Then I was running in to a problem with images and links printing out in plane text so the 3rd line was added to fix that problem.

This next four lines was added becouse I know not everone is going to want to add images and links to the feeds they are pulling in so I added this setting to let you remove them if you wanted.

  1.  
  2.  // This line removes all html in the feed
  3. if ($images_in_feeds == no){
  4. $content[0] = strip_tags($content[0]);
  5. } else{ }
  6. $my_feeds[] = $content[0];
  7.  

The next three lines are used to pull in the title of the feed. If you are looking to add a new part to pull in this is the best code to look at for a starting point. It is the three lines that are needed to pull in anything.

  1.  
  2. $the_title = explode("<title>", $end_feed[0]);
  3. $titlecontent = explode("</title>", $the_title[1]);
  4. $title_my_feeds[] = $titlecontent[0];
  5.  

These three lines are used to extract the link from the RSS feed. The same goes as above, I did not mod any of this data as it was not needed to be used.

  1.  
  2. // Pull In Link
  3. $link = explode("<link>", $end_feed[0]);
  4. $linkcontent = explode("</link>", $link[1]);
  5. $link_my_feeds[] = $linkcontent[0];
  6.  

Next I am pulling in the the time. This kind up jump around a little bit due to how I wanted to display the time in the finished product so I am going to go over it one at a time.

This is where I pull in the time

  1.  
  2. $feed_time = explode("<pubDate>", $end_feed[0]);
  3. $timecontent = explode("</pubDate>", $feed_time[1]);
  4. $timecontent[0] = strtotime("$timecontent[0]");
  5. $time_my_feeds[] = $timecontent[0];
  6.  

Now I change the time from being a stream of number witch is the the time in seconds of the post to being posted to how long ago in seconds it was posted.

Then I take that number and make is so I can display the time in weeks, days , hours and seconds.

  1.  
  2.         $time_agos = array(
  3.         $thedays,
  4.         $thehours,
  5.         $themins /*,
  6.         $thesecs */
  7. );
  8. foreach($time_agos as $time_ago){
  9. if ($theweeks<"1" && $time_ago>"1"  )
  10.         echo $time_ago;
  11. else
  12.         echo "";
  13. }
  14. if ($theweeks>"1")
  15.         echo $theweeks ; /*.$thedays This is out until I stop it from Showing 0 days */
  16. else
  17.         echo "";
  18.  

Well that is it for the most part, the only thing I have left out is where it all comes together. That is done with this bit of code.

  1.  
  2.     <?php
  3. $cnt=0;                                                                                                 //Control Feed count
  4. while (
  5.         (true && $cnt<$number_of_post) &&  
  6.         (list(, $the_rss_feeds) = each($my_feeds )) &&
  7.         (list(, $link_feedss) = each($link_my_feeds)) &&
  8.         (list(, $title_feeds) = each($title_my_feeds)) &&
  9.         (list(, $time_feeds) = each($time_my_feeds))
  10.  ){
  11.         $cnt++;                                                                                         //Control Feed count
  12.  // This Has to do with showing how long ago the post was
  13.         $interval = time()-$time_feeds;
  14.         $year   = $interval/29030400;
  15.         $month  = $interval/2419200;
  16.         $week   = $interval/604800;
  17.         $days   = $interval/86400;
  18.         $hour   = $interval/3600;
  19.         $min    = $interval/60;
  20.         $sec    = $interval/1;
  21.        
  22.         $frac_w                 = $weekfloor($week);
  23.         $frac_d                 = $daysfloor($days);
  24.         $frac_h                 = $hourfloor($hour);
  25.         $frac_m                 = $min  - floor($min);
  26.  
  27.         $theweeks       = floor ($week)                 .‘ Weeks ‘;
  28.         $thedays        = floor ($frac_w*7)             .‘ Days ‘;
  29.         $thehours       = floor ($frac_d*24)    .‘ Hours ‘;
  30.         $themins        = floor ($frac_h*60)    .‘ Minutes ‘;
  31.         $thesecs        = floor ($frac_m*60)    .‘ Seconds ‘;
  32. // Adds Unique Id to each Feeds
  33. $i = $feed_add_id++; ?>
  34.     <li class="feeds" id="<?php print ‘feed’.$i; ?>" ><span><?php echo $title_feeds ?>: </span> <?php echo $the_rss_feeds?> <a class="timespan" href="<?php echo $link_feedss ?>" target="_blank" title="<?php echo $title_feeds ?>">Post:
  35.       <?php
  36.         $time_agos = array(
  37.         $thedays,
  38.         $thehours,
  39.         $themins /*,
  40.         $thesecs */
  41. );
  42. foreach($time_agos as $time_ago){
  43. if ($theweeks<"1" && $time_ago>"1"  )
  44.         echo $time_ago;
  45. else
  46.         echo "";
  47. }
  48. if ($theweeks>"1")
  49.         echo $theweeks ; /*.$thedays This is out until I stop it from Showing 0 days */
  50. else
  51.         echo "";
  52. ?> Ago</a> </li> <?php } ?>
  53.  

Well that is it, if you have any questions just post in the comment and I will do my best to answer and if you use this code just say thanks!

Twitter Feeds With PHP (Basic)

In my last post I showed you a Twitter Feed that had a lot of options such as.

  • Changing display name
  • Pulling in Twitter image
  • Ever tweet having a unique id
  • Showing how long ago the post was
  • giving a link to the post
  • Making links shorter
  • And a little bit more.

If you are looking for a more advanced Twitter feed you can find that
HERE

But this one is just for the basic user. I pulls in the tweets, puts them in an li and that is it.
The code has went from 120 lines (I think) to about 30 lines of code.

I have also not added any style to the one at all.
You can view the demo
Here
And download the files
Here

So lets go over a little of the code now.

  1.  
  2.         $username = "James_Riter";
  3.         $limit = 50;
  4.  

That is the first two lines of the php code and it is where you will change the user name and how many tweets you wish to pull in.

  1.  
  2.         $feed = ‘http://twitter.com/statuses/user_timeline.rss?screen_name=’.$username.’&count=’.$limit;
  3. $ch = curl_init();
  4. $timeout = 5;
  5.         curl_setopt ($ch, CURLOPT_URL, $feed );
  6.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  7.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  8.         $file_contents = curl_exec($ch);
  9.         $tweets = $file_contents;
  10.  

The next 8 lines or so are just setting up the where the feeds are and using “curl” to go grad the page that has the feeds on them. It also has a time out so that if Twitter is down (Something that seems to happen often) It will not slow your site down to much trying to grab feeds of a down site.

  1.  
  2. $tweet = explode("<item>", $tweets);
  3. $tcount = count($tweet) – 1;
  4. for ($i = 1; $i <= $tcount; $i++) {
  5. $endtweet = explode("</item>", $tweet[$i]);
  6.         $title = explode("<title>", $endtweet[0]);
  7.         $content = explode("</title>", $title[1]);
  8.         $content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,100})[^\s<]*)/", ‘<a href="http://$2$3" >$4</a>’, $content[0]);
  9.         $content[0] = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $content[0]);
  10.         $mytweets[] = $content[0]; }
  11.  

All of this code is saying now that I have the feed look for something called “item” and inside item grad what is called “title” but skip the first one.

That takes you to the line where you can see the closing tag to title.

After that the next two line witch look like this

  1.  
  2. <ul id="twit">
  3. <?php while ((list(, $thetweets) = each($mytweets ))){ ?>
  4. <li class="tweets"> <?php echo $thetweets?> </li>
  5. <?php } ?>
  6. </ul>
  7.  

And that is it, now you are pulling in just normal tweets to your page.

If you want to see what the feed looks like that you are pulling in you can go
Here
and take a look at the code (chrome and FF just push ctrl+U) and that will give you an idea of what you can pull in.

Add A Twitter Feed With PHP (Advanced)

Here we are going to go over a little file that I have put together for pulling in Twitter feeds. You will see that in the title this is titled as “Advanced” and the reason for that is there is a lot that this file does.

To View A DEMO look
HERE.

Download my demo files
HERE.

I also have the files on Soutceforge for you to down load
Sourceforg Link

I have been working on this for a few days and over time will make updates. I have tried to pack as much as I could in to this little plug in with out making it to complex for the every day user. I think as long as you know css and basic html you will be able to work with this with no problem at all.

As I will go over latter you really only need to mess with three lines of code.
That is to change the Twitter user, change the user name and to pick how many tweets you want to pull in.

Also this will validate as XHTML 1.0 Transitional and the only thing stopping it from doing Strict is I have a target=”_blank” in the code. So that should be an easy thing to fix if you care about Validation and need Strict HTML for any reason.

So lets go over some code here. :)

  1.  
  2. $username = "James_Riter";
  3. $displayname ="James";
  4. $limit = 10;
  5.  

That is the first three lines of code and if you dont want to deal with php it is really the only code you will have to mess with other than the css I am guessing.
So it work like this.
Line 1: That is your Twitter Id
Line 2: That is the name that will show up in the tweet
Line 3: That is the number of tweets you will be able to pull in.

If you set those three lines and remove all my css you will be in good shape to style your new feeds.

  1.  
  2. // Checks if Twitter feed is good
  3. $checkfeed = curl_init(‘http://twitter.com/’.$username);
  4. curl_setopt($checkfeed, CURLOPT_RETURNTRANSFER, true);
  5. curl_exec($checkfeed);
  6. if(curl_getinfo($checkfeed, CURLINFO_HTTP_CODE) == 404) {
  7.         $username = "James_Riter";
  8.         $displayname ="James";
  9. }
  10.  

That bit of code is to make sure you never pull in a dead twitter feed. So if you have set the name is still see me then you know you have typed your name in wrong.
What it does is sees if you get a twitter 404 pages and if so defaults to my twitter feed.

  1.  
  2. $title = explode("<title>", $endtweet[0]);
  3. $content = explode("</title>", $title[1]);
  4. $content[0] = str_replace("&#8211;", "&mdash;", $content[0]);
  5. $content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", ‘<a  title="$4" class="linksfromtwit" href="http://$2$3" target="_blank">Check Out This Link!</a>’, $content[0]);
  6. $content[0] = str_replace("$username: ", "$displayname_fix", $content[0]);
  7. $content[0] = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $content[0]);
  8. $content[0] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $content[0]);
  9. $mytweets[] = $content[0];
  10.  

This bit of code is what pulls in the tween its self. There is not a lot you would want to change in here.
What is does is looks in the rss feed that we are pulling the tweets from and pulls out the title witch is the full tweet its self. then it grabs the links and puts them in to link form so we have something to work with.

  1.  
  2. $link = explode("<link>", $endtweet[0]);
  3. $linkcontent = explode("</link>", $link[1]);
  4. $linkmytweets[] = $linkcontent[0];
  5.  

This is what pulls in the links to the tweet its self. If you look at my demo you will see that the Time Ago is also a link. So I wanted to pull in a link to the tweet its self so I could link the two.

  1.  
  2. $twittime = explode("<pubDate>", $endtweet[0]);
  3. $timecontent = explode("</pubDate>", $twittime[1]);
  4. $timecontent[0] = strtotime("$timecontent[0]");
  5. $timemytweets[] = $timecontent[0];
  6. }
  7.  

This bit of code is what pulls in the time I had to add it to this next bist of code to de the out come I wanted.

  1.  
  2.  
  3. $interval = time()-$timetweets;
  4.  
  5. $year   = $interval/29030400;
  6. $month  = $interval/2419200;
  7. $week   = $interval/604800;
  8. $days   = $interval/86400;
  9. $hour   = $interval/3600;
  10. $min    = $interval/60;
  11. $sec    = $interval/1;
  12.  
  13. $frac_w         = $week     – floor($week);
  14. $frac_d         = $days      - floor($days);
  15. $frac_h                 = $hour      - floor($hour);
  16. $frac_m                 = $min  - floor($min);
  17.  
  18. $theweeks       = floor ($week)         .’ Weeks ‘;
  19. $thedays        = floor ($frac_w*7)     .’ Days ‘;
  20. $thehours       = floor ($frac_d*24)    .’ Hours ‘;
  21. $themins        = floor ($frac_h*60)    .’ Minutes ‘;
  22. $thesecs        = floor ($frac_m*60)    .’ Seconds ‘;
  23.  

When Twitter gives you the time it looks something like this “Mon, 01 Nov 2010 17:38:38 +0000″
and seeing that I wanted to show how long ago the post was I had to do a little extra work.
But what that code does for the most part is takes the time Twitter gave up and then convert it to a usable time. Now that I have a usable time I just do a little math game.

I take the seconds and divide them by what I need to use. “$years $months $weeks”

Then I take weeks and subtract it by what is called floor.
Say my weeks in seconds looked like this 1604800.34564
What floor does is gets rid of ever thing after the “.” so “34564″ is then gone.

Now that I have a nice number to work with I times it by the needed number.

The math problem would look a little like this..

My days in seconds
1604800.34564 / 86400 = 18.5740781
18.5740781 – .5740781 = 18
18 * 7 = 126 days

  1.  
  2. $userimage = ‘http://img.tweetimag.es/i/’.$username;
  3.  

This is used just for the twitter user image that gets pulled in.

Well there is more to it than just that I have worked hard to make a good plug in that does a lot for you with out having to set any thing up.

If you have any questions just let me know. As soon as I get a chance I am going to strip this code down to just the basics so that for those that would like it can just have a simple twitter feed.

Return top