Twitter API 101 – Authentication with PHP and cURL
January 23, 2009
Now that I’m in the process of writing a simple twitter application, I want to share with you some of the steps that might help you save some time and grief in the future.
In this post I’ll show you how to connect securely to Twitter, authenticate, make a query and return a particular users’ followers as a string using PHP and cURL.
//Create the connection handle
$curl_conn = curl_init();
//Set up the URL to query Twitter
$user_followers = “https://twitter.com/statuses/followers/”username.xml”;
//Set cURL options
curl_setopt($curl_conn, CURLOPT_URL, $user_followers); //URL to connect to
curl_setopt($curl_conn, CURLOPT_GET, 1); //Use GET method
curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication
curl_setopt($curl_conn, CURLOPT_USERPWD, ‘username:password’); //Set u/p
curl_setopt($curl_conn, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate (but use SSL of course), live dangerously!
curl_setopt($curl_conn, CURLOPT_RETURNTRANSFER, 1); //Return the result as string
// Result from querying URL. Will parse as xml
$output = curl_exec($curl_conn);
// close cURL resource. It’s like shutting down the water when you’re brushing your teeth.
curl_close($curl_conn);
Explanation
The first step is to decide the type of query to use. For more information see the API wiki. Then you will set up all the options for cURL. In PHP these are set up one by one using curl_setopt(). If you don’t need a particular option, you can comment it out. Each option is explained as a comment, but there are some areas that grant a bit more explanation.
Twitter uses basic authentication for their services. This is only needed for certain parts of the API, but more than likely you’ll end up having to authenticate at some point.
Use HTTPS if possible, otherwise credentials would be sent in clear text and it would be fairly easy to intercept and read.
Lastly, execute cURL. This is when the connection happens, until now we have only set options, but haven’t actually attempted a connection. Now that we have a string back, we can convert it to an XML object and parse it for any information we need.
Related Posts
15 Responses to “Twitter API 101 – Authentication with PHP and cURL”
-
-
Great, thank you, stupid twitter api wiki only provides that Oauth method, while basic authentication makes it much easier for users to use apps. They’d be wise if they’d provide this code themselves!
So thanks, great help:)
-
[...] Twitter API 101 – Authentication with PHP and cURL [...]
-
Hi,
if you can helpI am looking for any code that can make a simple search to twitter and get an answer
Thank You!
David -
Is there a code that can actually log me in to twitter without passing to the regular login page. Something similar with facebook connect.
Thanks
-
[...] We will be using PHP and cURL to access the API. If you aren’t too familiar, please read this post. [...]
-
bonjour,comment je peux activer curl sur wampserveur?
je veux exploiter Oauth on twitter dans mon application comment je peux l’utiliser?c très urgent,merci -
Hi, Is it also possible just to check the username and password without making a query? I want to check the username and password to get a true or false returned. (I did found an article on the web, but it didn’t work…)
-
Hi Nic, thanks for the reaction. I have an application to post messages to twitter, which works fine. But I want users to sign in first before they’re able to send a message. That way I want to make sure the message is posted on the right profile. So what I want is a code for users to identify themselves, and if they do, they’re able to post a message using the form I already made… I hope that’s more clear
-
Oke thanks for the info!










Thank you for the code sample, but I think that you have a small typo on line 13 and that instead of ‘username/password’ it should be ‘username:password’.
Arik Fraimovich’s last blog post..How To Create a Teapot with PHP