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