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

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

Switch to our mobile site