Source: http://apiwiki.twitter.com/
The Twitter API is a tool for developers to gain access to Twitter’s core data in order to build useful applications involving Twitter.
It is quite neat to be able to access live data and be able to visualize the tweets of the world (or trends) or be able to pool live data and stream it through one service. This post is just to show the simplicity, but provides developers the tools necessary to build out a really neat application.
Goal:
To pool data (Finding and populating all data using the trend test – #test) into a single Twitter account.
Twitter has many different wrappers for their API; you can find a full list here. (http://apiwiki.twitter.com/Libraries). We can use a wide variety of languages, but for this exercise we are using PHP.
The API that I am using is the Twitter class by Tijs Verkoyen – (you can download the API at:http://classes.verkoyen.eu/twitter/). I found the documentation really helpful and provides me with all of the calls that are needed for this example.
The first step is to figure out how to post to twitter using the API.:
include "twitter.php";
$twitter = new Twitter("username","password"); //replace username and password with your account's username and password;
$userText = "Testing Twitter API";
$twitter ->updateStatus($userText );
?>
If we run this, you can see in your Twitter account that you have just updated your status to say “Testing Twitter API”. Now you can pass in more parameters such as links or other Twitter accounts and Twitter will automatically provide links where necessary.
So now we are half way there; the next step we need to look at is to pool in all of the data. In our example we want to pool in all information related to the trend #test. Now this can be whatever you want. I am using the Twitter search to return the results I want. Now the Twitter search returns results as an ATOM RSS feed. We need to parse the RSS feed so that we can extract the core data. In this case I recommend an Atom Feed reader (http://www.the-art-of-web.com/php/atom/#source). We can extract the search results for #test.
include "atomparser.php";
$url = "http://search.twitter.com/search.atom?q=%23test";
$atom_parser = new myAtomParser($url);
$output .= $atom_parser->getOutput();
echo $output;
?>
We now have the code to read in the Twitter RSS feed and parsed all of the information. However we now have too much information, all we want is what people tweeted and post that. I found it easier to go through the Atom parser and comment out all of the unnecessary data. I added a line break (“
” ) to indicate every new post visually. I can break all of the line breaks and push them into an array for me to loop through and push those results to Twitter.
Now that we have both parts of the code we need to combine the two code so that we can post onto Twitter from the results we were given from the RSS feed.
include "twitter.php";
include "myatomparser.php";
//load in the RSS
$url = "http://search.twitter.com/search.atom?q=%23test";
$post_to_Twitter = new array();
$atom_parser = new myAtomParser($url);
$output .= $atom_parser->getOutput();
// initialize twitter
$twitter = new Twitter("username","password"); //replace username and password with your account's username and password;
//$userText = "Testing Twitter API"; //not necessary anymore we are posting from the results that we were given from the RSS feed.
$post_to_Twitter = explode( "
", $output );
foreach( $post_to_Twitter as $me ) {
echo $me;
$user_text = $me;
sleep(1);
}
$twitter ->updateStatus($userText );
?>
I added a delay – sleep(), to allow the Twitter API receive each result post, then receive the next result. And that is it we have now developed a small piece of code that grabs the data from the Twitter search results and post it onto your twitter status update.
enjoy.
P.S if you need help with the commenting part, you can download my version of the atom parser here( ).