In this post I want to illustrate a super simple way to work with Twitter API and PHP. In particular, this tutorial explains how to get public updates from Twitter public timeline and display them in a web page with a custom style using CSS. In order to get Twitter updates I used Twitterlibphp (a PHP implementation of the Twitter API) that allows you to take advantage of it from within your PHP applications.

Using this simple method you can obtain awesome results like this:

You can download the source code at the following link and reuse it for free in your web projects (you need PHP and APACHE):


1. A little introduction
Twitterlibphp returns Twitter timeline in XML format and with this structure (take a look at the Twitter API home page for a full list of available nodes.):

status

created_at
id
text
source
user 

name
screen_name
description
profile_image_url
url
followers_count

So, if you want to display the user image, user status, and status date in your timeline you have to choose the following nodes:

But how you can display them? It’s very simple! Take a look at the following code!

2. PHP code
Create a new file twitter_status.php and copy and paste the following code:

<div class=”twitter_container”>
<?php
// require the twitter library
require “twitter.lib.php”; 

// your twitter username and password
$username = “your_username”;
$password = “your_password”;

// initialize the twitter class
$twitter = new Twitter($username, $password);

// fetch public timeline in xml format
$xml = $twitter->getPublicTimeline();

$twitter_status = new SimpleXMLElement($xml);
foreach($twitter_status->status as $status){

foreach($status->user as $user){
echo ‘<img src=”‘.$user->profile_image_url.'”>’;
echo ‘<a href=”http://www.twitter.com/’.$user->name.'”>’.$user->name.'</a>: ‘;
}

echo $status->text;
echo ‘<br/>’;
echo ‘<div>Posted at:’.$status->created_at.'</div>’;
echo ‘</div>’;
}
?>
<div>

How you can see, the previous code is very simple to understand. The line:

$xml = $twitter->getPublicTimeline();

get the 20 most recent public statuses posted. You can also use other functions such as:

getFriendsTimeline(): returns the 20 most recent statuses posted by the authenticating user and that user’s friends.

getUserTimeline(): returns the 20 most recent statuses posted from the authenticating user.

getReplies(): returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.

Take a look at twitter.lib.php for the full list of available functions.

If you want to display different information, for example the source of the update (status->source) use this code:

$status->source

…or if you want to display the number of followers of the current user (follower_count) use this code:

$user->followers_count

3. CSS Code
Now you can customize the style of your timeline using CSS code. I used the following classes but you can customize the look how you prefer:

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

}

That’s all! Download the source code, open twitter_status.php, change $username and $password with your Twitter username and password and upload the file in your test server.
If you have some suggestion, please add a comment!

Source

karthikeyanPHPtwitterAPI,Cascading Style Sheets,php,twitterIn this post I want to illustrate a super simple way to work with Twitter API and PHP. In particular, this tutorial explains how to get public updates from Twitter public timeline and display them in a web page with a custom style using CSS. In order to...PHP, HTML5, CSS, jQuery, Angular, Facebook Style Scripts & demos

Related Posts