Bright Hub
 
Matthew Casperson's Hubfolio

Dispaying your Twitter status with PHP

Article by Matthew Casperson (4,911 pts )
Published on Nov 8, 2009

This tutorial shows you how to retrieve and display your twitter status with PHP.

Introduction

DOWNLOAD THE SOURCE CODE

Adding a live Twitter status display to a webpage is a quick way to tie in a static web page with the live social media and news provided by Twitter. This tutorial shows you how to use the Twitter REST API from PHP to obtain, format and display a Twitter status post.

PHP Twitter Screenshot

Step 1 - Define a URL Regular Expression

Twitter posts quite often include links. However, the Twitter API will only return a status update as text, without the HTML markup to identify a link. To get around this we will use regular expressions to turn URLs into clickable links.

The following is a simple Regular Expression that will match URLs in the Twitter status posts.

$url_re = '((https?|ftp|gopher|telnet|file|notes|ms-help):((\/)|(\\\\))+[\w\d:#@\/\%;$()~_?\+-=\\\.&]*)';

Step 2 - Define the Twitter REST API URL

The Twitter REST API URL is made up of a common base URL, the username of the Twitter user whose status will be retrieved, and the format of the returned result. You can choose from XML or JSON. Since PHP has built in support for JSON we will use that format.

$twitterUsername = 'mcasperson';

$twitterStatusURL = 'http://twitter.com/statuses/user_timeline/' . $twitterUsername . '.json';

Step 3 - Call the API

With the Twitter REST API URL in hand we can now call it using CURL. The code below is fairly typical of the process you use to call any REST based web service from PHP with the CURL library (in fact this code was copied straight from the Yahoo BOSS Custom Search with PHP article).

$session = curl_init($twitterStatusURL);

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$resultsJson = curl_exec($session);

curl_close($session);

Step 4 - Convert the returned JSON

The code returned by the web service above is JSON. This needs to be converted into a PHP object so we can easily reference the data.

$results = json_decode($resultsJson);

Step 5 - Add the HTML link markup

As was mentioned in step 1, the Twitter status posts are returned in plain text, even though they quite often contain links. Here we grab the status text and enclose any URLs in the text with the HTML <a> tag to make them clickable links using the Regular Expression replace function preg_replace.

$twitterStatus = $results[0]->{'text'};

$twitterStatus = preg_replace('%' . $url_re . '%', '<a href="$0">$0</a>', $twitterStatus);

Step 6 - Display the status

All that is left now is to actually display the Twitter status update in a HTML page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>My Twitter Status</title>

</head>

<body style="font-family:Arial, Helvetica, sans-serif;">

<p>

<?php

echo $twitterStatus;

?>

</p>

</body>

</html>

Conclusion

The Twitter REST API offers a lot more functionality than just getting status updates. The same process described here can be used with any other Twitter API function.

Return to the Tutorial Index

google docsSubmit data to a Google Form with PHP

This tutorial shows you how to submit data to a Google Spreadsheet with a custom PHP form.

Search More About:

Comment

Jan 18, 2010 12:21 PM
Chris
Twitter Status
Thanks. I intend to dig into Twitter's API to understand the process but needed a quick solution. Works great.
 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape