Dispaying your Twitter status with PHP

February 6th, 2010 by payam

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 .

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

source :: brighthub

Posted in Scripts | No Comments »

Support us

Your Donation Will Be Used To Pay For Hosting Service. Help Keep Our Web Site Online With a Donation!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.