Twitter PHP and SimpleXML feed

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn


Just like the Google Charts API the Twitter API can be super simple. Twitter uses a few different XML feeds to send and receive data from a given user, some feeds require a user name and password supplied through Curl but a few are publicly available to all.

This example will use the built in SimpleXML functions which come with PHP5, this is a much faster and easier way to using alternative readers such as simplePie or anything else you might come across. As far as it goes I don’t think SimpleXML gets enough coverage so I am happy to put it to some use for this example.

Keeping it basic

<?php

$xml = simplexml_load_file(“http://twitter.com/statuses/user_timeline/adi182.xml?count=2″);
if($xml){
echo ‘<h3><a href=”http://www.twitter.com/adi182”>Latest Tweets</a></h3>
<ul>’;
foreach($xml->status as $tweet){
$description = $tweet->text;
echo ‘<li>’.$description.'</li>’;
}
echo ‘ </ul>’;
}
?>

So there you have it, the super simple example on putting a Twiter feed on your website.


Posted by Adi on April 29, 2009

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

2 responses to “Twitter PHP and SimpleXML feed”

  1. Jack Lenox says:

    Hi there,

    I’m a bit befuddled by simplexml to be honest. Why does this approach not work if you take a normal RSS feed from Twitter? Why does it have to be this Atom method? (I’ve tried the former and it didn’t work)

    Thanks,

    Jack

  2. Brad Barwick says:

    @Jack, the user RSS structure is different from the ATOM API. Try this:
    foreach ( $xml->channel->item as $tweet )

Leave a Reply

Your email address will not be published. Required fields are marked *