HowTo – Twitter API Displaying XML Feeds & User Info With SimpleXML

Filed under: Web Design

twitter_logoTwitter provides a number of ways to redisplay their content and access their api so we can build plugins or full applications to access and  manage our own accounts or to retrieve public content of other users.

Although we are use to reading nicely formatted twitter pages from our web browser you probably never noticed that each user has an RSS Feed and a XML Feed associated with their account.
For this example we are going to take a look at Danica Patrick’s XML Feed and see what we can find.

You will find all of the info that is used to populate her twitter profile including her ID number which is important and images, name, location and so on.

In addition to her profile information you see her latest tweet.

Now the reason her ID number is important is that you can use it to grab her ATOM Feed so you can display her latest tweets.  You can use the following url and apply the same code as we are using for the XML feed or you can use another RSS type  Feed Reader.

Here is her ATOM Feed Url. Using her Id from her XML Feed as shown below.

It is important if you are looking for Twitter API Data to view the source of the ATOM Feed to find what data is missing from the XML Feed that you can capture from the ATOM or RSS Feeds.

One thing that is important to me is the URL of her larger Profile Image which is included in the ATOM Feed.. while a smaller version is included in the XML Feed.

OK Lets See the Code for grabbing the XML Feed.

Below you will see we are using curl and SimpleXML which should be parts of your PHP install to grab and extract the feed data.



function twitter_get_file($name, $credentials=false){
$res = ”;
if($credentials === false)
$res = @file_get_contents($name);
if($res === false || $res == ”){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $name);
if($credentials !== false)
curl_setopt($ch, CURLOPT_USERPWD, $credentials);

curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);

twitter_log(‘Failed to read feeds from twitter’);
return $res;

$username ="DanicaPatrick";

$api_url_reply = ‘’.urlencode($username).’.xml’;
$req = twitter_get_file($api_url_reply);
$xml = @new SimpleXMLElement($req);

$id = (string)$xml->id;
$name = (string)$xml->name;
$screen_name = (string)$xml->screen_name;
$location = (string)$xml->location;
$description = (string)$xml->description;
$profile_image_url = (string)$xml->profile_image_url;
$url = (string)$xml->url;
$protected = (string)$xml->protected;
$followers_count = (string)$xml->followers_count;
$profile_background_color = (string)$xml->profile_background_color;
$profile_text_color = (string)$xml->profile_text_color;
$profile_link_color = (string)$xml->profile_link_color;
$profile_sidebar_fill_color = (string)$xml->profile_sidebar_fill_color;
$profile_sidebar_border_color = (string)$xml->profile_sidebar_border_color;
$friends_count = (string)$xml->friends_count;
$created_at = (string)$xml->created_at;
$favourites_count = (string)$xml->favourites_count;
$utc_offset = (string)$xml->utc_offset;
$time_zone = (string)$xml->time_zone;
$profile_background_image_url = (string)$xml->profile_background_image_url;
$profile_background_tile = (string)$xml->profile_background_tile;
$notifications = (string)$xml->notifications;
$geo_enabled = (string)$xml->geo_enabled;
$verified = (string)$xml->verified;
$following = (string)$xml->following;
$statuses_count = (string)$xml->statuses_count;
$lang = (string)$xml->lang;
$contributors_enabled = (string)$xml->contributors_enabled;

echo "id: " . $id . "<br>";
echo "name: " . $name . "<br>";
echo "screen_name: " . $screen_name . "<br>";
echo "location: " . $location . "<br>";
echo "description: " . $description . "<br>";
echo "profile_image_url: " . $profile_image_url . "<br>";
echo "url: " . $url . "<br>";
echo "protected: " . $protected . "<br>";
echo "followers_count: " . $followers_count . "<br>";
echo "profile_background_color: " . $profile_background_color . "<br>";
echo "profile_text_color: " . $profile_text_color . "<br>";
echo "profile_link_color: " . $profile_link_color . "<br>";
echo "profile_sidebar_fill_color: " . $profile_sidebar_fill_color . "<br>";
echo "profile_sidebar_border_color: " . $profile_sidebar_border_color . "<br>";
echo "friends_count: " . $friends_count . "<br>";
echo "created_at: " . $created_at . "<br>";
echo "favourites_count: " . $favourites_count . "<br>";
echo "utc_offset: " . $utc_offset . "<br>";
echo "time_zone: " . $time_zone . "<br>";
echo "profile_background_image_url: " . $profile_background_image_url . "<br>";
echo "profile_background_tile: " . $profile_background_tile . "<br>";
echo "notifications: " . $notifications . "<br>";
echo "geo_enabled: " . $geo_enabled . "<br>";
echo "verified: " . $verified . "<br>";
echo "following: " . $following . "<br>";
echo "statuses_count: " . $statuses_count . "<br>";
echo "lang: " . $lang . "<br>";
echo "contributors_enabled: " . $contributors_enabled . "<br>";



When you execute the PHP  File you should see something like this:

id: 21255118
name: Danica Patrick
screen_name: DanicaPatrick
location: Phoenix, AZ
description: Driver of the #7, love to cook especially breakfast then workout running, weights, yoga,
protected: false
followers_count: 184090
profile_background_color: 000000
profile_text_color: 4a4537
profile_link_color: c91400
profile_sidebar_fill_color: d4d4d4
profile_sidebar_border_color: b50003
friends_count: 44
created_at: Wed Feb 18 23:28:33 +0000 2009
favourites_count: 120
utc_offset: -21600
time_zone: Central Time (US & Canada)
profile_background_tile: false
notifications: false
geo_enabled: false
verified: true
following: false
statuses_count: 603
lang: en
contributors_enabled: false

Now What?

Now that you have the Data you can format it however you want. You can make the page live so calls are made every time the page is loaded or you can place the data in a database.  If you have a popular site I suggest you cache the data at the very least so you don’t get banned by twitter for too many calls to their servers.