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/


46 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. :)

  16. Shinji wrote,

    Hi Philip, first and foremost I'd like to thank you for your wonderful PHP Twitter RSS Parser, you have no idea how many I've tried and each with its own problems, mainly with the cache, but yours is the closest I could find. Thank you so much.

    Would like to ask oyu a question with regards to displaying the dates of the twitter post, I noticed that if the post is within a day it would show how many hours ago, but if the post is more than a day it would show the time and date of the post. Where is the script that I can change so as to display the dates as how many days ago if the post is more than a day old?

    Thanks in advance, really appreciate you for this script. :)

  17. Philip wrote,

    @Shinji: I am glad you like the script. In answer to your question, you will need to edit the 'timesince' function within the file, 'functions.php'. Let me know how you get on. :)

  18. Philip wrote,

    @Shinji: I have had another quick look and I believe you could achieve what you want by editing line 35 within functions.php from:

    if($key == 'day' | $key == 'month' | $key == 'year'){
    

    to:

    if($key == 'month' | $key == 'year'){
    

    Hope this helps. :)

  19. Shinji wrote,

    Hi Philip,

    Thanks for such a fast reply, I've tried to change that value but doesn't seem to reflect any changes on the output side… hmm…

  20. Philip wrote,

    @Shinji: Hmm, strange, it works my end. :/

    See: http://crunchbang.org/what-am-i-doing/

  21. Shinji wrote,

    Hi Philip,

    Hmm, it doesn't seem to reflect on my side, it continues to show 10:26am September 14, 2009 … hmm… I didn't change any of your script other than limiting the feeds to 1 latest feed using what Cole Thorsen posted on Wednesday, March 4th, 2009… Could that be interfering?

  22. Shinji wrote,

    I did some changes to

    twitterzoid.php link 46

    $twittered_at = timesince(strtotime($item->get_date("Y-m-d H:i:s")),$item->get_date("j F Y"));

    To show the date format, for now, trying to make the hours ago or days ago show… >_<

  23. Paul Tepper wrote,

    Works well, thanks for putting this out there.

    I also modified it to allow for a limit on tweets.

        $items = $feed->get_items();
        if(!isset($twitter_max)) $twitter_max = count($items);
        $i = 0;
        foreach($items as $item):
            if($i<$twitter_max){
                $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."    
  24. \n"; $TwitterZoid=$TwitterZoid." ".$tweet.""; $TwitterZoid=$TwitterZoid." - get_permalink()."\">".$twittered_at."\n"; $TwitterZoid=$TwitterZoid."
  25. \n"; $i++; } else break; endforeach;
  26. Mark Andrews wrote,

    Thanks for this script, I played with it it works really well, should it work for search result feeds though ? I was struggling to do this.

    In fact I have not sucessfully managed to show a search RSS feed on a formated HTML page yet using PHP, any help would be great :-)

    Thanks

  27. Philip wrote,

    @Mark, I have not tried it with search result feeds. If I get time, I will have a play around with them and see what can be done. :)

  28. Paul wrote,

    Great script — thanks for putting this together. We're using at NYU - for example http://wagner.nyu.edu/grepin

    I've modified it to open all links in a new window.

    Also - I found what might be a bug. Twitter usernames can have underscores in them. I'm not sure what other special characters/punctuation are allowed — that's all I've seen so far. But I needed to add it to the regular expression to parse a name that came up:

    I think it's line 50 of functions.php

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

    I also added target='_blank' to the link.

  29. Philip wrote,

    @Paul, I think it is brilliant that the script is being used on some NYU pages, thank you for letting me know about it. Thanks also for the adjusted regular expression, I was not aware that Twitter usernames could have underscores in them. Doh.

  30. Crag wrote,

    Hey,

    great script got it uploaded on my server but i cant get the feed to show :S Do i have to change the permissions of any folders or files? or what?

    Also are you able to show multiple feeds?

    Thanks,

    ~Crag

  31. Philip wrote,

    @Crag, are there any error messages produced? Have you doubled checked the feed address?

  32. Crag wrote,

    well it just took its time to appear lol my bad

    Just wondering if theres away to show multiple feeds?

    Thanks,

    ~Crag

  33. Philip wrote,

    @Crag, not at the moment, other than having multiple instances of the script, or by adding additional features yourself. What did you have in mind and how do you envisage it working?

  34. Mark Andrews wrote,

    @philip , I dont suppose you had any joy with the search results request ?

    Thanks muchly.

    Mark

  35. Philip wrote,

    @Mark, no sorry. I have been somewhat busy of late and I have not gotten around to playing about with it yet. :(

  36. Shinji wrote,

    Hi Philip,

    Just a query, is it possible to disable @xxx replies in twitterzoid? I've looked through the codes and I can't seem to find a work around for that… =|

  37. Philip wrote,

    @Shinji, to clarify, do mean is it possible to stop the automatic linking of @replies?

  38. Shinji wrote,

    @Philip, yes, is it possible to stop the automatic linking of @replies… Still couldn't figure how to remove it… broke the script a couple of times… =(

  39. Philip wrote,

    @Shinji, in the functions.php file, comment out the line that starts

        $text = preg_replace("/(\s@|^@)([a-zA-Z0-9_]{1,15})...
    

    i.e.

        //$text = preg_replace("/(\s@|^@)([a-zA-Z0-9_]{1,15})...
    

    Hope this helps. :)

  40. Shinji wrote,

    @Philip, just tried it… it doesn't seem to work… =(

  41. Shinji wrote,

    @Philip, apparantly it seems that the @replies is cached thus it doesn't work?… Hmm…

  42. Philip wrote,

    @Shinji, some questions:

    Are you using Identi.ca or Twitter? Could you supply a link to the feed you are parsing? And, did you try deleting the cache before attempting the fix? It might be worth always deleting any cache files before testing your changes.

  43. Shinji wrote,

    @Philip,

    I'm using Twitter, on my own twitter feed as follows:

    http://twitter.com/statuses/user_timeline/9320712.rss

    Yes, I tried deleting the cache… =(

  44. richs-lxh wrote,

    Excellent script Philip!! I just added it as a page on my blog.

  45. Philip wrote,

    @richs-lxh, I am glad you have found it useful! :)

  46. blardyuk wrote,

    Thanks loads fr this script it was exactly hat i was looking for. Just one questions, I a totally dumb when it comes to PHP, i have got the script working fine but i cant get either of the limit scripts (above) to work. Any chance of an example implementation? or a copy and paste new code would be great!?

  47. Philip wrote,

    @blardyuk, I will look into it tonight. If you do not hear back from me, please feel free to give me a virtual poke via my contact form. :)

  48. Willy T. Koch wrote,

    Thanks for a great class! I also had the cache warning in the logs.

    I changed the ./cache path to an absolute path (/home/users/blah/www/twitterzoid/cache) and gave it 777 in permissions, deleted the old cache. The error went away and cache files were written OK.

    Regards,

    Willy T. Koch Norway

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.