Wednesday, February 20th, 2008

TwitterZoid PHP Script

It's a rather silly name, I know; however, TwitterZoid is the chosen name of my PHP script for parsing Twitter RSS feeds. I've been using Twitter quite steadily for a couple of weeks now and I thought it might be nice to include my latest tweets on my blog, so I wrote TwitterZoid to do just that.

TwitterZoid differs to other PHP based Twitter RSS parsers, at least the ones I tried before I wrote it, in that it will automatically link both lexicons and URLs found within individual tweets. It also tries to mimic Twitter's timestamping, although this could be improved.

TwitterZoid usage

I wanted to make TwitterZoid as simple to use as possible. Therefore I decided to write the script to be used as a simple include file which can be used on any PHP page. Basically, to use TwitterZoid all you need to do is set a couple of variables, include twitterzoid.php and then echo the main $TwitterZoid variable where you would like your list of tweets to appear.

Example set-up:

$twitter_username = "corenominal";
$twitter_feed = "http://twitter.com/statuses/user_timeline/99713.rss";
require_once('twitterzoid.php');

Call on the main TwitterZoid variable to produce the list of tweets:

echo $TwitterZoid;

Don't worry if this reads like gibberish, I've included an example page within the download.

TwitterZoid examples

There are currently two demonstrations of TwitterZoid in action, see:

My official "What am I doing?" Twitter page:
http://crunchbang.org/what-am-i-doing/

A more stylised version of "What am I doing?", included within the download:
http://crunchbang.org/projects/twitterzoid/demo/

Download TwitterZoid

Location: http://crunchbang.org/projects/twitterzoid/twitterzoid-0.2.tar.gz
MD5: 7c437c2ea32f45dde66fc74f690ab361

TwitterZoid license

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

http://www.gnu.org/licenses/


15 Responses to “TwitterZoid PHP Script”

  1. fiskpinnen wrote,

    I really like the fact that TwitterZoid is telling me that crunchbang-7.10.02.i386 is going to be downloaded to a computer near me very soon!

    Keep up the excellent work man!

  2. Becky wrote,

    I'm really NOT liking the fact that my fiance "Twitters" about getting naked to everyone - especially when I'm not there to scrub "His Royal Highness" ;-)

  3. Philip wrote,

    @Becky: Babe, it's hardly everyone. Besides, how do you know I didn't Twitter it hoping that you'd rush home from work to scrub my… hmm, I think I'll stop there before I get myself into more trouble =D

  4. gouki wrote,

    Heheh! Well, that made me laugh! Good day to both of you!

  5. Flywheel wrote,

    I'd like to try, but Winzip reports "invalid archive directory" for the download on 2/18/09.

  6. Philip wrote,

    @Flywheel: Thank you for the heads-up, I will look into it.

  7. Cole Thorsen wrote,

    Hey,

    Love the script, I needed to give it the option to limit the number of tweets so I thought I would share it.

    Just requires a little change to the twitterzoid.php file:

    $i=1;
    foreach($items as $item):
        if(!isset($twitter_numberOfTweets) || $i <= $twitter_numberOfTweets){ 
            $tweet = $item->get_description();
            twitterit($tweet,$twitter_username);
            $twittered_at = timesince(strtotime($item->get_date("Y-m-d H:i:s")),$item->get_date("g:ia F j, Y"));
            $TwitterZoid=$TwitterZoid."    <li>n";
            $TwitterZoid=$TwitterZoid."    ".$tweet."<span class="tweetmeta">"; 
            $TwitterZoid=$TwitterZoid." - <a href="".$item->get_permalink()."">".$twittered_at."</a></span>n";
            $TwitterZoid=$TwitterZoid."    </li>n";
            $i++;
        }
    endforeach;
    

    You will also have to add the variable $twitter_numberOfTweets = 5; whereever you are calling twitterzoid (it is completely optional, if you don't declare this it will parse the entire rss feed and display it.

    Nice plugin overall good work.

  8. Jim wrote,

    Love the script - but get the error in the following lines of code in my installation:

    // Cache the file if caching is enabled if ($cache && !$cache->save($this)) { trigger_error("$cache->name is not writeable", E_USER_WARNING); }

    I just commented theses lines out so the error wouldn't show up, but am curious as to why this error occurs. I am using an Apache server.

  9. Sergei wrote,

    @Jim: I got the same error on an Apache server and after a few minutes of playing around with it I couldnt figure it out and just ended up doing the same as you and commenting it out.

  10. RWM wrote,

    Nice work! TwitterZoid works wonderfully well for me for one application I have, but for another little project I'm working on, the applicable Twitter account (& therefore the RSS feed) is protected. Is there a straightforward way to make TZ work in this case?

  11. Philip wrote,

    @RWM: The script is quite basic and is only really designed to work with freely accessible feeds. It could probably be modified to work with a protected feed?

  12. Kose wrote,

    Very cool script, thank you! And @Cole Thorsen thank you too, i was looking exactly for that feature! :)

  13. D Brown wrote,

    Great work. Thanks!

    By simply replacing the feed with Twitter search atom feed, I was able to display all Tweets with a particular hashtag. The anchor tags within the Tweets weren't working correctly so I had to change line 78 of functions.php to be:

    $value = $key;
    

    How would you suggest I also show the user name and icon for each Tweet? Perhaps I should just use a different script all together, but yours works so well I thought I'd ask.

    Thanks!

  14. Pepino wrote,

    Cool and easy script. THX.

    Two little bugs/improvements:

    1. underscores in usernames let the script not link correct.
    2. is there a way to link the hashtags too?

    TIA Pepino

  15. Philip wrote,

    @Pepino:

    underscores in usernames let the script not link correct.

    I have not tested this, but try altering line 50 in the functions.php file from:

    $text = preg_replace("/(s@|^@)([a-zA-Z0-9]{1,15})/","$1<a href="http://twitter.com/$2">$2</a>",$text);
    

    to:

    $text = preg_replace("/(s@|^@)([a-zA-Z0-9_]{1,15})/","$1<a href="http://twitter.com/$2">$2</a>",$text);
    

    is there a way to link the hashtags too?

    This should be achievable by creating another regex line similar to line 50 above. Something like the following should do it, although again, I have not tested it:

    $text = preg_replace("/(s#|^#)([a-zA-Z0-9_]{1,15})/","$1<a href="http://www.hashtags.org/tag/$2">$2</a>",$text);
    

    If you try the above, please let me know how you get on. :)

Add Your Comment

Use the form below to add your comment. Markdown syntax is available. Note, comments are moderated by me for spam filtering. Alternatively, feel free to contact me privately.