Messing with the Twitter API

Today I was reading a bit of the Twitter API Wiki to better understand the methods and to see the possiblities of 3 methods:

What I have found out is that, if you are using cURL (and as they would say, you should), it is fairly easy.

Search: – Does not require authentication

$search = urlencode(‘Messing with the Twitter API’);
$curl = curl_init(“http://search.twitter.com/search.atom?q=”.$search.”&rpp=1″);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$xml = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection

echo $xml;

This will return one result for the query “Messing with the Twitter API”. The search query will be set at the q= querystring and the number of tweets to be returned will be set by the rpp= querystring.

Update

$params = array(‘status=’ . ‘Messing with the Twitter API’);
$postFields = implode(‘&’, $params);
$curl = curl_init(“http://api.twitter.com/1/statuses/update.xml”);
curl_setopt($curl, CURLOPT_USERPWD, ‘yourTwitterUsername:yourPassword’);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$xml = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection

echo $xml;

Just like the search is fairly simple. The params holds the message that you want to post and the authentication goes under the cURL option.

Retweet

Retweet is very similar to the Update method, the only difference is that you cant retweet your own tweets and to retweet something, you must have a status id to be retweeted.

$curl = curl_init(“http://api.twitter.com/1/statuses/retweet/5466220198.xml”);
curl_setopt($curl, CURLOPT_USERPWD, ‘yourTwitterUsername:yourPassword’);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$xml = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection

echo $xml;

In this case the 5466220198 is the id of the status that you are retweeting.

All of 3 methods are fairly simple, but don’t go busting up on Twitter API, check the wiki because there restrictions on the number of tweets that you can do in a hour and besides that, checking the Wiki will help on more methods, parameters, response formats and forms of calling the method (such as JSON).

Have fun.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

One response to “Messing with the Twitter API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: