Twitter PHP and SimpleXML feed

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.

2 thoughts on “Twitter PHP and SimpleXML feed

  1. 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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>