So, I started writing a Twitter API Programming series last week… Here is part 1. I have found that there is not much else to teach when it comes to interfacing with Twitter. Basically, we just need to implement all of the Twitter functions.
I have taken it upon myself to create a fully functional Twitter API PHP class. It has every Twitter function you will ever need (less the direct messages). I have also fully documented it with comments directly from the Twitter API so that the functions are easy to understand. Here is how you would use the class. The first parameter of every function is the return type. For Twitter it can be either xml, json, or rss for some functions.
include("Twitter.class.php"); $twitter = new Twitter(); $twitter->username = "twitteruser"; $twitter->password = "twitterpass"; // Show public timeline echo $twitter->public_timeline(); // Update your status echo $twitter->update('xml','This PHP class is flippin sweet!'); // Start following a user echo $twitter->create('xml','brandontreb');
These are just 3 of the functions implemented in this class. The full function list includes:
- public_timeline
- friends_timeline
- user_timeline
- update
- replies
- friends
- followers
- show
- create
- destroy
- exists
All of these functions are fully documented and simple to use. You can also check out Twitter’s API here (but you shouldn’t need to).
So, now you can fully integrate Twitter into any PHP application you create!
Download The PHP Twitter Class here Twitter.class.php
Also, make sure you subscribe to my RSS feed. More great programming tutorial to come.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

[...] The Only Twitter API PHP Class You Will Ever Need from Brandon Treb. [...]
Pingback by Twitter PHP Class « $i++ — February 27, 2009 @ 12:35 pm
How does this work? I have tried create and destroy functions but it didn’t work for me.
Comment by Riz — March 3, 2009 @ 3:20 pm
the username & password doesnt work!
Comment by daniel — March 6, 2009 @ 3:35 am
@Riz What do you mean?
@daniel Make sure you set it to YOUR username and password. These are just sample ones….
Comment by brandontreb — March 6, 2009 @ 10:10 am
It was working three days ago – until I think Twitter has blocked this somehow because the same demo which was working doesnt work anymore – I am getting Error 403 – you can see it here – http://www.ukluxe.com
Comment by L Mohan Arun — March 9, 2009 @ 3:42 am
OK I found out what the problem is – if I am logged in using a Twitter client and use the library to make status updates then it doesnt work. I logged out of Twitter client and then use the library on the server then it works -
Comment by L. Mohan Arun — March 10, 2009 @ 1:58 am
@L. Mohan Arun.
That is a good point (only being logged into one Twitter client at a time). I will include that in an update of this post. Thanks for pointing that out.
Comment by brandontreb — March 10, 2009 @ 3:07 pm
Hello,
Twitter replies are password protected, what should I do?
http://twitter.com/statuses/replies.xml
Best Regards,
Andrei Luca
Comment by Andrei Luca — March 17, 2009 @ 10:29 am
That line 7 of your example should be
echo $twitter->public_timeline(‘xml’);
methinks?
Comment by Ajax Jones — April 24, 2009 @ 4:05 pm
I got a:
Error: 403
When I ran this.
Comment by Rob — April 25, 2009 @ 1:08 pm
I also get a 403 error.
Comment by Al — April 27, 2009 @ 10:58 am
The 403 Error is a permissions error. This means that you entered an invalid Twitter Username and Password combination. Make sure you change the “username” and “password” in my sample code to YOUR Twitter username and password. More on this error explained here http://www.checkupdown.com/status/E403.html
Comment by brandontreb — April 27, 2009 @ 3:47 pm
How do you use the returned XML and display it in a formatted way? (New to this)
Comment by Adam — May 2, 2009 @ 1:46 am
This is what returned when I ran a status update script:
How can I customize it to say “success” or something?
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/www/vhosts/twittearn.com/httpdocs/Twitter.class.php on line 164
Mon May 25 06:39:34 +0000 2009 1910227828 This PHP class is ballz! web false false 28748812 Brayden Williams redstar504 http://static.twitter.com/images/default_profile_normal.png false 3 9ae4e8 000000 0000ff e0ff92 87bc44 1 Sat Apr 04 05:42:25 +0000 2009 0 http://static.twitter.com/images/themes/theme1/bg.gif false 7
Comment by Brayden Williams — May 24, 2009 @ 11:40 pm
This is fantastic – thank you very much for releasing it into the open!
Comment by LL — May 30, 2009 @ 3:55 pm
I also get the error :
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set.
Is there a work around ?
Comment by Erwin — June 11, 2009 @ 2:43 pm
When I try the sample (note password is intentionally commented out)
username = “jtmckim”;
$twitter->password = “*********”;
// Show public timeline
echo $twitter->public_timeline(‘XML’);
?>
I get the error message:
“Fatal error: Call to undefined function curl_init() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Twitter.class.php on line 152″
Any ideas would be greatly appreciated.
James
Comment by JTM — June 12, 2009 @ 3:25 pm
@Adam Use some sort of PHP xml parser. Just Google it, there are a ton out there.
@Erwin Sounds like PHP is running in safe mode. Changes this in your HTTP.conf file
@JTM This means that you don’t have the CURL libraries installed. You need to download them and recompile PHP with them.
Comment by brandontreb — June 13, 2009 @ 9:48 pm
Thanks for your response. Adding the CURL extension to php seems to have eliminated the error. However, now all I get a blank page.
James
Comment by JTM — June 17, 2009 @ 8:52 am
@James,
1. Are you outputting anything?
2. Is error reporting turned on for PHP?
Sorry these might seem obvious, but I just want to make sure.
Comment by brandontreb — June 17, 2009 @ 9:06 am
Sorry, but I gave the output from a different php file. I’m now getting an Error 406 when I make a the call to public_timeline() and an Error 404 when calling user_timeline().
James
Comment by JTM — June 17, 2009 @ 9:16 am
BTW, in playing around a bit, when I call followers(), I get results just fine.
James
Comment by JTM — June 17, 2009 @ 9:35 am
Ok, for those of you having trouble with the class. I have updated the code and it should now be a little more stable.
Comment by brandontreb — June 24, 2009 @ 7:40 am
works fine for me, got it to grab the XML data from all my timelines, and parse them into nice looking lists. Added a time since function, got it to link replies and hashtags, and added a convenient update edit box. makes for a great little way to check my timelines when I’m in school; they block Twitter.com. For PHP 5 users, use SimpleXML:
Code:
You then have a nice object, $timeline, to play with, to make it format as lists, headings, whatever you want. You obviously need to know your way around the API data that is returned.
Comment by James Scholes — June 27, 2009 @ 3:57 pm
Thought I’d just give a brief example, as I noticed some comments were asking hwo to begin:
The following code will pull the public timeline, then place it in an HTML list. It is assumed you’ve created an object called $twitter using the code:
Now, it doesn’t look pretty, it hasn’t got many fancy features, heck it doesn’t even tell you when the tweet was posted. However, it should get you going in the right direction. If it doesn’t work, correct the characters that WordPress messed with, then if something still goes wrong, feel free to contact me. I’m not foolish enough to post my email address on a public blog, so just leave a comment on here and I’ll reply.
Comment by James Scholes — June 27, 2009 @ 4:50 pm
Oh dear. I ddidn’t realise how badly that would turn out. I have posted the source code for that example< as well as corrected a mistake I made over here.
Comment by James Scholes — June 27, 2009 @ 4:58 pm
@James,
Thanks for posting the code for the readers. I intend on doing a parsing tutorial at some point. Just a little pressed for time atm.
Comment by brandontreb — June 29, 2009 @ 12:04 pm
was just wondering, how come you haven’t implamented direct messaging? The API for reading and sending DM’s is pretty simple, as simple almost as the API calls you are using in the class at the current time. If you simply didn’t have the time, then fair enough. Was just wondering if there was a technical reason…
Comment by James Scholes — July 1, 2009 @ 2:19 pm
Yea, just haven’t had time. Also, I coded pretty much everything else so it should be pretty obvious how to do DMs.
Comment by brandontreb — July 1, 2009 @ 2:44 pm
Thank you. best information for me.
Comment by sunee — July 2, 2009 @ 2:16 am
I cannot get this to work. I’m just trying to update status:
[code]
$twitter = new Twitter();
$twitter->username = "u";
$twitter->password = "p";
echo $twitter->update('xml', 'testing');
[/code]
But it always gives me a 400 Error. I also tried uncommenting this line in the process() function, but it gives me a 417 Error:
[code]
//curl_setopt($curl_conn, CURLOPT_POST, 1); //Use GET method
[/code]
I have no idea why this wouldn’t work. I’m not getting any PHP errors or warnings. Any ideas?
Comment by P.J. — July 21, 2009 @ 2:22 am
How can I know username and password inputted are correct or not?
Thanks
Comment by AQ — July 22, 2009 @ 4:19 pm
@P.J according to
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
status updates must be POSTed. This class uses GET and would need quite a bit of rewriting to use POST, from what I can see. — but I’m no expert.
Comment by rich — August 4, 2009 @ 2:20 pm
@P.J @rich Sorry about that. The class has been updated to use POST. Download it again and give it another shot.
Comment by brandontreb — August 4, 2009 @ 2:47 pm
Are you aware google thinks this of your site?
http://safebrowsing.clients.google.com/safebrowsing/diagnostic?client=Firefox&hl=en-US&site=http://www.ukluxe.com/
Comment by Adam Arnold — August 7, 2009 @ 2:30 am
Just out of curiosity… should that download link work?
Comment by Adam Arnold — August 7, 2009 @ 2:38 am
Yea, thanks for pointing that out. Something must have gone wrong when I uploaded it. The download link is now fixed and you can download the class. Sorry about that.
Comment by brandontreb — August 9, 2009 @ 9:30 pm
Trying to get a list of followers I get a Error: 400. Any ideas?
$twitter = new Twitter();
$twitter->username = “u”;
$twitter->password = “p”;
// Update your status
echo $twitter->followers();
Comment by geoff — August 14, 2009 @ 9:23 pm
can you do an example showing the use of in_reply_to_status_id to send a status update in reply to a person you are following please?
Comment by mk — August 20, 2009 @ 8:05 am
Hi From France,
This class seems to work much better than the code I implemented before. Thanks a lot !
Two questions :
1/ I regularly get some 408 errors (once out of 2 or 3 calls). Any idea why ?
2/ I would like to avoid getting results on screen when doing an udpate ?
Best regards,
Thomas
Comment by Thomas — September 10, 2009 @ 2:33 am
1 – This is an issue with Twitter. Their servers have been terrible lately.
2 – Don’t print them
Comment by brandontreb — September 10, 2009 @ 1:53 pm
Great, thanks a lot. I am waiting for an answer from Twitter API folks.
Regards,
Thomas
Comment by Thomas — September 11, 2009 @ 6:47 am
First of all, AWESOME tutorial. The tweet works but the add friend does not – http://snixed.com/twitter/twit.php
Comment by Jeff — September 25, 2009 @ 10:59 am
Hi, thanks for the code
One small point – since XML is probably going to be the default for most folk, might be better to make the format the last parameter so you don’t have to keep on specifying it when you call the class. So the code above would become
// Update your status
echo $twitter->update(‘This PHP class is flippin sweet!’);
// Start following a user
echo $twitter->create(‘brandontreb’);
The folk can specify a format only when they want other than XML.
Anyway, thanks again.
Comment by Derek — October 6, 2009 @ 11:02 am
Jeff, I also get Error 302 when I use the create function.
Comment by Mark Bennett — October 7, 2009 @ 3:37 pm
a very awesome script. thanks for sharing
Comment by Duncan — October 12, 2009 @ 11:42 pm
[...] I just created a minimalist example to test the interaction between PHP and Twitter API, using the great Brandon Treb’s Twitter API class [...]
Pingback by Simple interaction between Twitter API and PHP | Part .01 | Chrometaphore tech blog — October 21, 2009 @ 4:15 am
your code still has a problem. Inside the foreach loop your referencing the $timeline variable instead of $status variable. It should say something like this :
foreach($timeline->status as $status) {
echo $status->name . ” -” . $status->text;
}
Comment by Omar M — October 23, 2009 @ 1:13 am
Hi, i am using your class to fetch my status posts and then i am importing them into FLEX, would would be very awesome, is for the php class to be able to re-format the date and also turn links into actual links with the <a href tags..
Awesome work, thanks.
Comment by steven — November 11, 2009 @ 8:53 pm
Awesome class! I would suggest adding the following function to the class:
function rate_limit_status($format=’xml’) {
$request = “http://twitter.com/account/rate_limit_status.$format”;
return $this->process($request);
}
Comment by Adam — November 25, 2009 @ 7:49 pm
I’ve saved this one for later. Thanks for the share!
Comment by Joe — December 5, 2009 @ 10:13 pm
Wow! This is awesome. Thanks for this!
Comment by Joe Longstreet — January 15, 2010 @ 10:53 am
I got an error 0? how could I resolve this? thanks
Comment by Muffinboy — January 18, 2010 @ 10:22 pm
Hi,
I am installed cUrl from http://packages.ubuntu.com/lucid/amd64/curl/download for my ubuntu 9.0 with php 5.2 ,Apache 2.0.It was installed by gkebi package installer. but when i display phpinfo() curl not found since i was attempt to use twitter class.
what is the problem i have?
When i am trying to install again package installer shows same version of curl already found, but twitter class shows following error
Fatal error: Call to undefined function curl_init() in /var/www/twitter/Twitter.class.php on line 152
Regards
Ramakrishnankt
Comment by ramakrishnan — January 19, 2010 @ 12:55 am
how to recompile PHP with curl?
Comment by ramakrishnan — January 19, 2010 @ 12:58 am
I got solution from curl-and-php mailing list for installing curl:
sudo apt-get install php5-curl
More help can be found here:
https://help.ubuntu.com/community/InstallingSoftware
Thanks
Ramakrishnankt
Comment by ramakrishnan — January 19, 2010 @ 4:22 am
Hey, this is great, but it doesnt appear to support any way to SEARCH – Is there a solution for php for searching twitter by timestamp and keyword?
Comment by Jonathan — January 21, 2010 @ 3:12 pm
Is there a way to update the Status of a twitter account from a PHP application for a user that is already logged in on the PC, without having to ask the user for their username/password ?
This exact scenerio can be done on Facebook to update a user’s wall if he is already logged in.
Comment by MikeU — January 27, 2010 @ 8:20 am
Hey Brandon, i think you are an fake, since i saw your
lines
foreach($timeline->status as $status) {
echo ’screenname.”\”>”.$timeline->name.”".$timeline->text.”";
// end foreach loop
}
which should be (as you call all $status) $status->name and $status->text..
But most of all what i dont like is that “your” class is exactly the same as David Billingham’s
http://twitter.slawcup.com/twitter.class.phps
Its not neat!
Comment by Quirilles — January 30, 2010 @ 7:49 am
I use this class and all is ok, but i have a little problem with the function $twiterra = $twitter->friends_timeline(‘xml’); error: 400
Comment by Unai — February 1, 2010 @ 4:50 am
Yeah, I agree with Quirilles. This class is exactly like David Billingham’s with more comments and a format argument. And it’s obviously not tested. The least you could do is acknowledge the original author.
Comment by Joe — February 10, 2010 @ 10:12 am