DEVseo

Making It Your Web

How To Use The Twitter API

  • Posted 17th Aug 2009 @ 15:28:15
  • Last update 24th May 2013 @ 18:01:34
  • By Alex Hall
  • In / PHP // Tutorial /

How To Use The Twitter API

There are a few examples on the web about using the Twitter API, but I've noticed that more and more often they refer to using exec() commands in PHP which a lot of web hosts deny to their users automatically. This makes using the API a little tricky as the only other good method for using the service is with cURL.

cURL is a PHP library written by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. This means you can use it from your web server to request documents from another web server and do all sorts of things with the response.

The Twitter API supports cURL requests and the easiest way to use these requests will be shown just below. It is also worth checking the Twitter API Documentation, which contains all of the functions they support as well as plenty of information on the sorts of things you can pass to the feed, and the response you will receive.

On To The Examples

The first thing to do is create a new PHP document (that is a text file with the extension .php). Open the PHP tag with:

<?php

And close with:

?>

In between these tags we'll be doing our cURL request and handling of the response. So, to initiate a cURL function you will need the following line:

$ch = curl_init();

This begins the initiation for a cURL request. I use $ch as the variable simply because it stands for 'Curl Handle', which is what we are opening. Once we have started the cURL we can then add all sorts of functions and parameters to the request, which includes specific post variables such as a username and password if Twitter requires those. The example I will show has a few parameters but does not require a password to be sent. I will come to that later in this tutorial. For now we'll need the following:

 
curl_setopt($ch, CURLOPT_URL, 'http://search.twitter.com/search.atom?q=hello');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 90);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

So, what does all this mean? Well, starting with the first line, this basically tells the cURL where to post the data to. It needs to be a URL to a valid page, preferably one that expects to be hit, such as REST services such as the one that Twitter has.

The next line is not necessary as such, but basically tells cURL how long to attempt to connect to the given URL before timing out and throwing an error. In this case I have set it to 90 seconds, but this is quite a lot and depending on your script you probably want to set this to a lot less than that. Usually about 10 is enough, it just depends how long you'd wish you page to take to load in the event that the cURL request times out.

The next line regarding 'CURLOPT_RETURNTRANSFER' refers to whether or not the cURL Handle should return whatever it recieves from the request. I set this to 1 (or yes) because I use the response in my callback functions and it gives you an indication of whether or not the request was successful depending on what you are requesting and from whom.

The final part simply tells the cURL request that we are sending post data and to send it as just that. You obviously don't need this if you are sending data straight to a script that doesn't require post methods.

The only thing that's left to do from here is to execute the cURL request and then close the cURL connection and check for a response when the request has been sent. The first part, executing the cURL handle is done by using the following line:

$return = curl_exec($ch);
curl_close($ch);

Here, because I requested that cURL returns something to my script I have set the response to a variable so that I can use it later on to do some checking. The rest simply executes the cURL and sends the data off, with the given headers, to the URL that we set up at the beginning. And the last line simply closes the cURL connection.

What Do We Do With The Data Retrieved?

The method I use for this is PHP Dom, simply because it's what I know and what I find easiest. That involves putting the response from the cURL handle into a DOM Document and then parsing that in PHP to retrieve the values I need.

The other method I have used is to send the request to json, rather than atom, which involves changing the URL slightly (see below) and in return we get a response in a json format that I use with the YUI library json parsing to retrieve the values I need. I will go through both of these methods.

curl_setopt($ch, CURLOPT_URL, 'http://search.twitter.com/search.json?q=hello');

To be continued...

Comments (1)

w2eZrhGMPp by Aline

on 6th Sep 2013 @ 15:01:28

Please teach the rest of these internet hoialgons how to write and research!

0 0

Reply

Add A Comment

Please note: all comments are moderated before they are published on this page.


skkhf7
Cancel Reply

Loading

Complete!