PHP">Retrieving Twitter’s Public Timeline with PHP

I am just play­ing around with Twitter’s PHP Lib­rary twit­ter­libphp and cre­ated this little application.

You can see the res­ults here.

Below you can see the code that pro­duces this simple example application.


<div class="twitter_container">
<?php

// Inlcude the Twitter Library
require_once 'twitter.lib.php';

// Instantiate the Twitter Object
$twitter = new Twitter("DanTheMan_nz", "lordofcrow666");

// Make the call to get the public timeline
$public_timeline_xml = $twitter->getPublicTimeline();

// Wrap it in an XML element
$twitter_status = new SimpleXMLElement($public_timeline_xml);

// Iterate through the xml objects
foreach ($twitter_status->status as $status)
{
// Iterate through the user objects
foreach ($status->user as $user)
{
// Bring the user's image on the screen
echo '<img src="' . $user->profile_image_url . '">';

// and the user's name
echo '<a href="http://www.twitter.com/' . $user->name . '">'.$user->name.'</a> :';
}
// Print out the status
echo $status->text;
echo '<br />';

// and the date it was created
echo '<div>Posted at:' . $status->created_at . '</div>';
}
?>
</div>

And here is the css code to style the whole thing a little bit.


.twitter_container{

color:#444;
font-size:12px;
width:600px;
margin: 0 auto;

}
.twitter_container a{

color:#0066CC;
font-weight:bold;

}
.twitter_status{

height:60px;
padding:6px;
border-bottom:solid 1px #DEDEDE;

}
.twitter_image{

float:left;
margin-right:14px;
border:solid 2px #DEDEDE;
width:50px;
height:50px;

}
.twitter_posted_at{

font-size:11px;
padding-top:4px;
color:#999;

}

It is pretty self explan­at­ory. If you still have a ques­tion you are wel­come to leave a ques­tion in the com­ments.
This example is based on Ant­o­nio Lupetti’s tutorial which can be found here.

Categories: Coding, PHP, Twitter Tags: , ,
  1. June 20th, 2011 at 02:38 | #1

    Hi,
    seems simple and fine, just the res­ult seem to be much more soph­ist­ic­ated (are those threads?) than the code, so I sup­pose, you made some updates in the mean­time that are not reflec­ted in this article.

  2. August 4th, 2011 at 00:38 | #2

    where do i place the css code and how do i ref­er­ence it into my php file.
    Thanks in advance…

  3. Unfor­given
    March 10th, 2013 at 04:23 | #3

    Hi, and thanks for this tutorial. Though I’m exper­i­en­cing a prob­lem, after try­ing your code, I get this error :
    Fatal error: Uncaught excep­tion ‘Excep­tion’ with mes­sage ‘String could not be parsed as XML’ in C:\wamp\www\Tests\Twitter\page.php on line 21

    And :
    Excep­tion: String could not be parsed as XML in C:\wamp\www\Tests\Twitter\page.php on line 21

    This is my first time ever try­ing to use such a lib­rary, under­strand that i’m a little lost now.
    Any help or hint would be most welcome.

    Thanks again, and good evening.

  4. May 31st, 2014 at 16:05 | #4

    Very rap­idly this site will be fam­ous among all blog­ging and site-building
    vis­it­ors, due to it’s good articles

  1. No trackbacks yet.