Your open source for Development and Design

Twitter PHP and SimpleXML feed

Filed under: API's — Tags: , , — Adi @ 2:45 pm April 29, 2009

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

$user = ‘adi182′;
echo ‘<a href=”http://www.twitter.com/’.$user.’”>Follow ‘.$user.’</a><br />’;
$xml = simplexml_load_file(“http://search.twitter.com/search.atom?q=from:”.$user.”&rpp=5″);
foreach($xml->entry as $tweet)
{
echo $tweet->title. “<br />”;
}
?>

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

1 Comment »

  • 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

    Comment by Jack Lenox — July 27, 2009 @ 6:45 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Switch to our mobile site