How to display Twitter updates using PHP (and without using curl)

This is something I needed to do recently. The following code reads a user timeline from Twitter (I’ve chosen the RSS format) and puts the result into a <ul> block. I’ve added a bunch of comments since PHP isn’t what I’d call a self-explanatory language.

<?php
$doc = new DOMDocument();
 
# load the RSS -- replace 'lylo' with your user of choice
if($doc->load('http://twitter.com/statuses/user_timeline/lylo.rss')) {
  echo "<ul>\n";
 
  # number of <li> elements to display.  20 is the maximum
  $max_tweets = 10;    
 
  $i = 1;
  foreach ($doc->getElementsByTagName('item') as $node) {
    # fetch the title from the RSS feed. 
    # Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
    $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
 
    # the title of each tweet starts with "username: " which I want to remove
    $tweet = substr($tweet, stripos($tweet, ':') + 1);   
 
    # OPTIONAL: turn URLs into links
    $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
          '<a href="$1">$1</a>', $tweet);
 
    # OPTIONAL: turn @replies into links
    $tweet = preg_replace("/@([0-9a-zA-Z]+)/", 
          "<a href=\"http://twitter.com/$1\">@$1</a>", 
          $tweet);
 
    echo "<li>". $tweet  . "</li>\n";
 
    if($i++ >= $max_tweets) break;
  }
  echo "</ul>\n";
} 
?>

 

Posted by Olly on February 4, 2009 Comments (14) | Permalink | Comments feed

14 comments on “How to display Twitter updates using PHP (and without using curl)”

  1. Stuar wrote on February 27th, 2009 at 4:07 pm:

    Most useful, thanks!

  2. GTweet is born at Adam Jarret Blog wrote on March 2nd, 2009 at 4:36 pm:

    [...] – I’m used to Cocoa), but I have now switched to using the PHP DOMDocument object. Check out this page as a [...]

  3. Stanford wrote on April 17th, 2009 at 2:02 am:

    This is very useful. It works great on my local machine. However, when I pushed it live to the web, it worked the first time and then stopped working. No tweets are returned. When I added “www.” to the url of my page, it worked again. Hitting refresh, it no longer worked. It’s as if Twitter blocks access to the feed after one request from a certain URL…

  4. Phil wrote on May 18th, 2009 at 10:16 am:

    Thanks for this! works perfectly!

  5. Agent wrote on June 3rd, 2009 at 1:42 am:

    The script is great. How would I display the date like in the following example?

    Jun 1st – @JoeBob Twitter message

  6. Agent wrote on June 3rd, 2009 at 1:45 am:

    The script is great but how would I display the date like in the following example?

    Jun 1st – @JoeBob Twitter message

  7. Great Tutorial wrote on July 19th, 2009 at 1:42 am:

    This is a great script. I have had some fun with it by adding other variables to the script, allowing me more control over which items to display (i.e. the date, the title, etc.) and how to display them. Thanks again!

  8. Tracy wrote on September 8th, 2009 at 9:34 pm:

    Hi. I have added small features to this script for my blog. Can I have your permission to post the script on my blog. I will give you the credit for the original work. Sorry for the comment but I don’t see any contact information to email you.

  9. marco wrote on September 23rd, 2009 at 3:33 pm:

    Hello!
    Great script, thank you!
    It’s possible to integrate hashtag support?

    Greetings from barcelona :)
    marco

  10. Mike wrote on October 8th, 2009 at 6:56 pm:

    I am having the same problem as Stanford. It works the first couple times but after a few refreshes, I get

    Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/*myfeed*.rss) [function.DOMDocument-load]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /mnt/w0802/d03/s39/b02d9397/www/my_page.php on line 5

  11. droppxiel wrote on March 9th, 2010 at 10:04 pm:

    Epic! In the process of redoing my site and this is a perfect replacement for what I was using before.

    I had used the script from instant shift, but when testing, the @userids links weren’t playing nice…

    Found this through that site!

  12. Kate BP wrote on May 22nd, 2010 at 8:19 pm:

    Brilliant code, thanks for posting it!

  13. Display Your Twitter Posts On Your Site! | World O Web wrote on May 26th, 2010 at 3:34 pm:

    [...] may require javascript to work. After trying a few scripts I came across a little PHP script from The Lylo Files that displays up to 20 of your posts on your web site. After using it for a little while I decided [...]

  14. The Twitter customer testimonial « CagedEther: Corporate Blogging, Twitter Advice, Social Media Management in the B2B Space wrote on September 16th, 2010 at 7:00 pm:

    [...] The next step is to take the RSS feed from this page and build a page on your site that displays this in a tidy Twitter format. If you are non-technical, ask your IT team to knock something up or look into modules that can do this for you. If you have a site built in PHP, you can try adapting this script from Lylo. [...]

Make a comment