Brian Moreau

Search this website


Plasma
Home
|
Solutions
|
Portfolio
|
Articles
|
Projects
|
Photography
|
Technology news
|
Blog
|
Contact
 
 
 
Twitter feeds:  

I decided I would like to learn how to display Twitter data on a webpage.
This had been on the list of things to do for quite a while but was finally motivated into action after seeing a twitter feed on the Sky News website.

Now there are in existence I would probably think 100’s if not 1000’s of existing applications or widgets already in existence that you can either download for free or buy from a number of sources, twitter included, however I always like to do things myself for several reasons.

Firstly learning is of most importance, secondly the free applications or widgets are never quite styled how I would like and generally there is always a catch, often the widget contains adverts if free. And lastly I am not going to buy something that I can have the satisfaction of creating myself.

How to build a twitter feed:
I consulted the API files on twitter which list the available methods or functions.
Each method has the following main fields.
Method description:
The method description basically describes what that method does.

URL:
This is the URL to use for the query or function.

Formats:
This is the returned data format. i.e. json, atom, xml

HTTP Method:
This is the form method you should use for the data transfer.

Requires Authentication:
Some functions such as posting a tweet obviously require you to be logged into your twitter account.  If you need to be logged in or authenticated then this will be true otherwise it will be false.

API rate limited:
This is the frequency you may use the function. To limit the web traffic to twitter servers they have introduced limits on functions that request information using the HTTP GET command. The default limit is 150 requests per hour.
Twitter state your application should recognise when it is being rate limited and thus be able to detect the HTTP 400 response code. They also suggest the use of auto detecting rate limits in your application and throttle requests if nesary.

The search API apparently has an undisclosed limit in excess of 150 requests per hour however when using the search API you must include a unique and identifying User Agent string. If the search API is used without this identifier the call rate will be reduced.

Should the limit be reached using the search method then a HTTP 420 response will be sent along with a Retry-After: xx which must be honoured.

How to do it:
While this information is invaluable and very useful and includes examples of the actual data returned I could not find any actual examples of how to decode the data.
I have had some experience of creating and decoding XML but json was a new term for me.

PHP being my favourite language I quickly looked this up in the inline PHP reference manual which unfortunately had no twitter examples but I did learn that json data appeared to be in the form of an array that PHP supports.

Now arrays come an all shapes and sizes and in order to decode one correctly you ideally need to know the format of the data.
I looked at 100’s of online examples which used the twitter API’s however al were very different and most simply didn’t work.

I decided to examine the json data array myself which can be easily done in PHP with the use of the following command.

PHP command to examine json data.
Print_r($my-json-data);
A typical result returned for the trends API is displayed below. For simplicity only two results of the possible 10 are displayed and I have separated the data onto separate lines..

An example of json data returned using the twitter trends API
stdClass Object (
[trends] => Array ([0] => stdClass Object (
[name] => #FF
[url] => http://search.twitter.com/search?q=%23FF
 )

[1] => stdClass Object (
[name] => #nowplaying
[url] => http://search.twitter.com/search?q=%23nowplaying
)

From the data above we can determine the name of the data array is [trends].
It is also clear there are two items of data called [name] and [url].
Now to be honest I am unsure exactly how to access a specific item in the array directly however we can simply loop through the array to retrieve each item in sequence and then retrieve each item data.
 
PHP code to display each item of data.
            foreach ($my-json-data->trends as $dataname) {
            echo $dataname -> url ;
            echo $dataname -> name;
            }

You will see that the data contains a URL for each trend which when followed displays the latest tweet for that search item.

An example twitter search URL
http://search.twitter.com/search?q=%23NowPlaying the %23 is a # so the search term in this example is #NowPlaying

Make it pretty
We may now manipulate the data as we please and display in any format we like.         
I like to use tables which aids keeping the data neatly lined up and it also allows you to make custom borders for the application.

 
 
 

Forward:
My twitter trends feed does not detect HTTP 420 or 400 response codes as yet but that functionality will be added as soon as I get time to program.

I will also later explain in detail how to use other feed functions such as search.

You may also like to read my article on “is their a point to twitter”  

I am also working on a project that allows control of a device connected to a computer by tweeting it. See http://www.brianmoreau.com/network/electronics.php for this.

 
 
© 2008 Brian Moreau