How to retrieve your status message from twitter using cURL?


what is cURL, or what is php? ok, PHP is a server side scripting language, which helps make webpages more intelligent than when they are written with plain HTML.

cURL is a command line tool for transferring files using a URL syntax. It is a free software, distributed under the MIT License. May be thats why its ” ‘c’URL ” – command(line) URL??

you can head here and here to find detailed write ups about these two.

Retrieving updates from Twitter is easily accomplished using php’s cURL library. What one needs is a localhost(typically xampp) with php and cURL for php enabled (and a working internet connection).

Step1 – Initialise cURL variable

$curl_handle=curl_init();

Step2 – Tell the cURLvariable the URL which it has to access.

$url=’http://twitter.com/statuses/user_timeline.xml’;

Set this variable into the cURL handle.

curl_setopt($curl_handle, CURLOPT_URL, “$url”);

STep3 – Set a few other options.
One, we are going to ask the cURL handle to wait for a minimum of 5 seconds for the Twitter server to respond.

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);

Two, we are going to tell it to access the url that we have set it to and get the results of the url back to us, so we can store it in a string. This option has to be explicitly set. Otherwise it will not bring back any results, it will trigger the url and comeback without the answer.

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

Step4 – Give the curl handle the username and password so that it can fetch your updates.

curl_setopt($curl_handle, CURLOPT_USERPWD,”$username:$password”);

Step5 – Set the curl handle on the task.

$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

Since we asked curl handle to get the results back to us, we are collecting it in the variable $buffer. And then we close the handle.

Step6 – Now we check if the $buffer variable has got anything at all. If it has, we will process it, otherwise we need to show an error message.
The comments in the code explain the various parts –

if (empty($buffer)) {
echo ‘error in fetching data’; //you can customize this error msg
}
else
{
else {
//We set the url to an xml file, so our result is in xml. Let us read the
//xml and display it.
//The easiest way to handle xml in php is to use this library function.
//This is a famous library, and mostly comes installed
//and turned on in xampp.

$xmlobj=simplexml_load_string($buffer);

// Initially I had no idea of what the xml file would contain so I just did
//the print_r() line below.

//print_r($xmlobj);

// And when I found that the xml has been converted to a
//multidimensional array (yay!) I just navigated through the array

$x=count($xmlobj->status);
//print “no of status updates are “.$x.” “;
for($i=0;$i<$x;$i++)
{
echo ($i+1).”. “;
echo $xmlobj->status[$i]->text;
echo “\n”;
}
echo ‘success’;
}

You can see this script at work here.

Advertisements

About vatsalad

Hi, I'm Vatsala
This entry was posted in Code Samples, How To... and tagged , , , , . Bookmark the permalink.