Hands on: How to build a simple Twitter reader
This article is excerpted from the book Sams Teach Yourself the Twitter API in 24 Hours with permission of publisher Pearson/Sams. Copyright 2011 by Pearson Education Inc., all rights reserved.
In this article, you'll learn to create the basic file structure that you would see with any typical website: the HTML, CSS, and PHP code to create the content. As we go through the steps, more advanced programmers may be wondering why we took certain approaches to our coding or choose to use what would seem overly simple techniques. After much debate by the writers and advisors, we decided that at the price of elegant coding, we would try to make things more clear to the beginning programmers.
Let's put our simple framework in place. It's a basic framework compared to more sophisticated applications. Our basic setup will have an HTML file that is called when the user first comes to the site, which will load main.php, as well as call three other files, each designed to perform a certain task, as you can see in the chart at left.
We are going to create six files: the HTML page, the CSS file, and four PHP files. Three of the PHP files will live in a subdirectory called includes.
Creating index.php and main.css
First, let's create the HTML code. You may have noticed that we are using index.php instead of index.html. Depending on your server's configuration, you can use either one, however convention is to use index.php if you have a php code in your file, and we very much do in our example. Create a new file called index.php in your editor and type in the following code:
<meta charset=utf-8>
<html>
<head>
<title>twitterAPI 24 hours</title>
<link href=css/main.css rel=stylesheet />
</head>
<?php
include 'includes/twitterAPI.php';
include 'includes/parseTwitter.php';
include 'includes/render.php';
?>
<body>
<div class=header>
<div class=tweet>TwitterAPI24</div>
</div>
<div class=container>
Next, we make a call to main.php, which we will use to load our content:
<? include 'main.php'; ?>
</div>
</body>
</html>
Creating main.css
It's not in the scope of this article to explain how CSS works, so we will simply provide the CSS code here. Create a folder called css and then create a file in that folder called main.css. Type in the following code:
/* Global Layout */
body {
margin:0;
padding:0;
font:12px Arial, Helvetica, sans-serif;
color:#999;
width:100%;
}
/* End of Global Layout */
/* Global Styles */
/* Disable CSS Text Decoration Property */
a {
text-decoration:none;
color:#667;
}
/* Enable CSS Text Decoration Property and select underline */
a:hover {
text-decoration:underline;
}
/* End of Global Styles */
/* Section: Header */
.header {
margin:0 auto;
padding:10px 10px;
background:#333;
overflow:hidden;
}
.tweet {
padding:0 10px;
}
/* Section: Table */
/* Format the Detail Table with border */
#detail_table td{
width:330px;
padding-top:2px;
padding-left:5px;
padding-bottom:2px;
vertical-align: top;
color:#333;
border-bottom:1px solid #DBDBDB;
font-family: Trebuchet MS, Arial, Helvetica, sans-serif;
valign: top;
}
/* Section: Messages */
/* Format the Message container */
.container{
float:left;
width:320px;
}
/* Format the Message image */
.mess-pic {
background-color:#eee;
padding-right: 5px;
font-size: 10px;
float:left;
}
/* Format the Message container */
.mess-container{
float:left; width: 250px; padding-left:10px;
}
/* Format the Message content */
.mess-row-text{
float:right; padding-left:10px; width:250px;
}
Great. Now that we have the presentation layer out of the way, we can get to the actual code. We are going to create four more files: main.php, parseTwitter.php, render.php, and twitterAPI.php.
Creating main.php
First, let's start with main.php. This file will make calls to all other functions we need to support our simple Twitter client application. The main.php script should not do any tasks in and of itself, but instead serve as the traffic cop for calling other functions. In this example, main.php is going to make three calls: first to callTwitter() from twitterAPI.php to get the latest messages from the Twitter servers; then it will call parseTwitterReply () from parseTwitter.php to convert the returned data into something more usable within PHP; finally, it sends the formatted HTML code of renderTweets() of render.php to the user's browser.
To get started, let's create main.php and type in our first line of code:
<?php
In the next line, we manually defined $twitterName. BREAKINGNEWS is the name of a Twitter account. You could replace this with any valid Twitter account with public tweets. We appended .xml to let Twitter know we want the response to our request to be in XML format:
$twitterName = 'http://api.twitter.com/1/statuses/user_timeline/BREAKINGNEWS.xml';
Next, we make a call to a function called callTwitter() in our twitterAPI.php file. It is here that we make the actual call to the Twitter servers. We will create this file later.
$twitterRequest = callTwitter($twitterName);
After we get our return from the callTwitter() function, we call parseTwitterReply in our parseTwitter.php file to convert the XML text reply we got from Twitter into a structured PHP object that is easier to work with. Again, do not worry; we will create this file later.
$twitterRequest = parseTwitterReply($twitterRequest);
After we have our reply from Twitter in a format we can use, we make a call to renderTweets in render.php to create the HTML code we will send to the user's browser:
echo renderTweets($twitterRequest);
?>


- Excel 2010 Cheat Sheet
- Register for this Computerworld Insider Cheat Sheet and gain access to hundreds of premium content articles, guides, product reviews and more.
- Workload Automation Challenges and Opportunities
- This Executive Brief discusses IDC's perspective on how enterprise workload management requirements are changing and highlights the ways that workload automation solutions can...
- Practice Management: Double Billing Rate and Improve Patient Services
- Would you like to double your billing rate and achieve faster payment for services?
Download this customer success story to see how One Health... - Mission Critical Data Explosion and Customer Case Study
- Would you like to double your tier 1 storage capacity while simultaneously reducing your storage footprint?
Download this customer success story to see how... - Protecting Against Database Attacks and Insider Threats: Top 5 Scenarios
- Read this new eBook to learn the top five scenarios and essential best practices for preventing database attacks and insider threats.
- Database Activity Monitoring Is Evolving
- Read the analyst report and learn how you can leverage the core capabilities of a DAP solution for better database security. All App Development White Papers
- Distributed Database Security with Real-time Monitoring
- View this demo and learn how IBM InfoSphere Guardium database activity monitoring can help protect your sensitive data in distributed DBMS environments with...
- InfoSphere Warehouse Packs Demo
- These flash modules make warehousing more tangible and relevant to business users through detailed explanations of the InfoSphere Warehouse Packs.
- Delivery Management -- Extending Lifecycle Management
- Date: Wednesday, June 20, 2012, 1:00 PM EDT
Siloed organizations continue doing the wrong things and doing things wrong, leading to increased costs,... - Leverage automation today to reduce IT complexity
- Date: Tuesday, June 5, 2012, 2:00 PM EDT
Whether your B2B complexity is caused by multiple technologies due to M&A, business or application specific... - Redefine Expectations in the Data Center
- Need to do more with less? Watch this video to learn how HP ProLiant Gen8 servers can help your business deploy servers three... All App Development Webcasts