Twitter offers 2 types of authentication. Using the standard cURL GET HTTP protocols or the new and fancy OAuth. cURL is easy, we all get it, and it works great. However, OAuth offers a wide range of fancy features, most importantly, securtiy.
Taken from the
OAuth website –
An
open protocol to allow secure API authorization in a simple and standard method from desktop and web applications. It’s a terrific idea and I’m glad someone is coming out with it. To make things easier there are also developers who’ve already done the work for you. Here’s a
pre-built script that does all the OAuth for you. There’s also a
Beginners Guide.
When the benefits are increased security, a higher level of compatibility, and new fancy technology, why bother with cURL anymore?
It’s pretty simple. cURL is easier, and it doesn’t require anything beyond what we know. Setting everything up with OAuth takes a few hours to wrap your head around. Compared to figuring out OAuth, here’s how to connect with cURL.
| 01 |
|
| 02 | $loginUsername = "username";
|
| 03 | $loginPassword = "password";
|
| 04 | |
| 05 | $url = "https://twitter.com/statuses/followers/" . $username . ".xml";
|
| 06 | |
| 07 | $curl_handle = curl_init();
|
| 08 | curl_setopt($curl_handle, CURLOPT_URL, $url);
|
| 09 | curl_setopt($curl_handle, CURLOPT_HTTPGET, 1); //GET method
|
| 10 | curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //basic authentication
|
| 11 | curl_setopt($curl_handle, CURLOPT_USERPWD, $loginUsername . ":" . $loginPassword); //login
|
| 12 | curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
|
| 13 | curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); //Return the string
|
| 14 | $buffer = curl_exec($curl_handle);
|
| 15 | curl_close($curl_handle);
|
| 16 | |
There you have it, you're logged in, or at the very least authenticated. Here's how to report back:
| 1 |
|
| 2 | // check for success or failure
|
| 3 | if (empty($buffer) || strpos($buffer, "Could not authenticate you")) {
|
| 4 | $replyStatus = "Failed: Please check your username and password";
|
| 5 | } else {
|
| 6 | $replyStatus = "Success: You are logged in.";
|
| 7 | }
|
| 8 | |

Twitter currently supports both without offering many benefits to either choice. cURL isn't really anything new but it's simple while OAuth will continue to develop. For serious developers who plan for longevity with their Twitter Application sites, I highly recommend taking the extra effort to at least understand OAuth enough to switch to it when they demand it. In the meantime, forget it and stick with cURL until the benefits outweigh the cons.
Was this useful? Help share it:
September 22nd, 2009 at 12:58 AM is there a code that can automatically login me in twitter?