Posts Tagged ‘Twitter’

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