Twitter API 101 – Authentication with PHP and cURL
January 23, 2009
Stop right there! I wrote this post back in January 2009, a lot has changed since then, and in case you haven’t heard basic authentication is a no go. I haven’t written any OAuth tutorials yet, so if you’re looking for some help check out the following links
How to Authenticate Users With Twitter OAuth
How to quickly integrate with Twitter’s OAuth API using PHP
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.
17 Responses to “Twitter API 101 – Authentication with PHP and cURL”
-
-
Great catch, thanks! It’s been corrected.
-
-
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-
David, have you looked at the search API page? It’s very complete and has a bunch of examples.
http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search
-
-
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 Lami, thanks for stopping by. I don’t use Windows, but it seems the answer to your first question is as follows
Click the WAMP icon in task tray, then go to PHP > PHP Extensions > php_curl. Then restart apache. The entire discussion is here http://www.phpfreaks.com/forums/index.php?topic=180453.0
As per OAuth, I haven’t had a chance to play with it yet, but there are a lot of resources out there.
-
-
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 Nick, I’m sorry if you received my previous reply, I had misunderstood your question. Are you trying to check whether a user’s credentials are valid? If you explain a little bit more of what you are trying to accomplish I might have some suggestions.
-
-
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
-
Crystal. Basic authentication is going to be deprecated soon, so you will want to look into OAuth. http://apiwiki.twitter.com/OAuth-FAQ
If you happen to still use basic auth and have the user’s username and password, you can use account/verify_credentials http://apiwiki.twitter.com/Twitter-REST-API-Method:-account%C2%A0verify_credentials
It returns HTTP status codes that will help you create your own boolean check. 200 + user info if valid, or a 401 if invalid. The start of the response will look something likeHTTP/1.1 401 Unauthorized Date: Tue, 10 Nov 2009 15:16:24 GMT
I go into a bit more detail here http://nicolasrosental.com/2009/11/10/twitter-response-codes-simplified/
I will very likely be writing a post on OAuth soon, but I haven’t had a chance to hack at it yet.
-
-
Oke thanks for the info!
-
Hi! I used your class and it was helpful! but there’s a way to get the user’s email address from twitter api. such as in facebook api?
thank you
Speak up
















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