Twitter class
Twitter is a service for friends, family, and co-workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question: What are you doing?
Grab your copy
About
PHP Twitter is a (wrapper)class to communicate with the Twitter API.
License
Twitter is BSD licensed.
Documentation
The class is well documented inline. If you use a decent IDE you'll see that each method is documented with PHPDoc.
Sites that use this class
- Broken Frontier
- De Bloedgroep
- WordPress Live Blogging plugin
- Symfony Twitter plugin
- Netlash
- Talking Heads
- CodeIgniter Twitter Library
- Personal site of Davide Favia
Do you know a site that uses this class? Use the form below.
Comments
Add your comment
If you have some feedback, a remark or you just want to say something. Go ahead, use the form below.
Tijs Verkoyen wrote:
31/07/2009
A basic tutorial is available on http://www.thelancelife.com/how-to/howto-tweets-from-php/
Paul Matthews wrote:
31/07/2009
Hi
Think there may be a problem in the Twitter wrapper with using (int) to cast the $sinceId values, because the Twitter IDs are now past the maximum int size of 2 billion or so.
E.g. I was using getReplies, and think I have fixed the problem by casting the sinceId to a float instead of an int on line 612
Love the class BTW!
Paul
Tried to post this on your twitter API page, but got an error "something went wrong" !!
Tijs Verkoyen wrote:
31/07/2009
Fixed the problems with the int. sinceIds are treated as a string from now on.
Schanie wrote:
01/08/2009
Is there a php plugin or a php code to use a twitter search box on a blog or a web site.
Tijs Verkoyen wrote:
01/08/2009
I think the API-methods for search are moved into the Twitter API, I'll try to include those methods into the next release of this class.
TheLanceLife wrote:
01/08/2009
Thank you for suggesting our simple tutorial :)
Would be interesting to write another one with search functionalities since we did some experiment with search using another simple php lib but would be great to have everything packed together.
If you have any beta to test or review let us know and most of all keep up the great work!
Thank you
Howie wrote:
01/08/2009
Any plans to add oAuth support?
Tijs Verkoyen wrote:
02/08/2009
Changelog since 1.0.3
- rewrote some comments
- fixed some PHPDoc
- it seems Twitter removed the $since-parameter, so I removed it from getFriendsTimeline, getUserTimeline, getDirectMessages, getSentDirectMessages, ...
- implemented maxId into getFriendsTimeline
- renamed getReplies to getMentions to reflect the Twitter API
- added $count for getDirectMessages, getSentDirectMessages, ...
- added getFriendship which shows more details about a friendship
- added getFriendIds and getFollowerIds which return only the ids instead of a user-array
- added existsBlock which test if a block exists
- added getBlocked, which returns an array of blocked user-arrays
- added getBlockedIds, which returns an array of blocked ids
REMARK: the class isn't backwards compatible. See the changelog if the methods you're using are changed.
Valerie wrote:
03/08/2009
Hi,
Unfortunately your script has the following problem when using it on a shared host:
Warning: curl_setopt_array() [function.curl-setopt-array]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /location on line 185
/direct_messages/sent.xml Could not authenticate you.
Fatal error: Uncaught exception 'TwitterException' with message 'Unauthorized' in /location/twitter.php:225 Stack trace: #0 /location/twitter.php(864): Twitter->doCall('direct_messages...', Array, true, false) #1 /location/test.php(9): Twitter->getSentDirectMessages() #2 {main} thrown in /location/twitter.php on line 225
Tijs Verkoyen wrote:
03/08/2009
Valerie, this has nothing to do with shared-hosting-setups. The warning is triggered because you're working with safe-mode on.
What you can try: comment line 154 (the line that sets the CURLOPT_FOLLOWLOCATION-option).
Hope this works
Valerie wrote:
04/08/2009
Hi,
Thanks for you reaction.
It works without line 154, but it seems proper to add this line.
Safe mod was already switched off and open_basedir has been set on my home dir at shared hosting. Still giving the problem with line 154.
Tijs Verkoyen wrote:
04/08/2009
Valerie, take a look at http://php.net/manual/ro/function.curl-setopt.php#71313. There seems to be a workaround.
Alex wrote:
07/08/2009
Hi,
I'm use your script.
But I got this error one day ago:
Fatal error: Uncaught exception 'TwitterException' with message 'Operation timed out after 120 seconds with 0 bytes received' in /home/wire/public_html/include_new/twitter.php:213 Stack trace: #0 /home/wire/public_html/include_new/twitter.php(583):
I have increased the time in twice (from 60 to 120).
but it is not help me.
Could you help me with the problem?
Thanks,
Alex
Tijs Verkoyen wrote:
07/08/2009
Alex,
The error means the connection to the Twitter API timed out. Nothing I can do about that.
Mark wrote:
09/08/2009
Hi. I have adapted your great PHP Twitter API Class as a CodeIgniter Library: http://codemuscle.com/downloads/codeigniter/twitter
Of course, I have credited you and left the BSD licensing message in the file.
Thanks for a great library.
Vanleurth wrote:
09/08/2009
I'm trying to use your code to change twitter background but I keep getting a "Not Implemented" error.
Is this documented somewhere? I looked through the internet and could not find anything. Any links or answers is appreciated.
Thank you,
Tijs Verkoyen wrote:
09/08/2009
@Vanleurth: The error means, the method is not implemented. I wrote about that on my blog: http://blog.verkoyen.eu/blog/p/detail/php-twitter-why-i-didnt-implement
David Jones wrote:
13/08/2009
I see your Twitter class includes "getMentionsReplies" that retrieves mentions of "@username".
I would like to do something similar and retrieve mentions of a particular word like "#special".
Can you tell me how to do that in PHP using your Twitter class?
thanks in advance, ...
Kamlesh Patidar wrote:
14/08/2009
Hi Tijs Verkoyen ,
i got foll0wing error, i am using your API..
Fatal error: Uncaught exception 'TwitterException' with message 'couldn't connect to host' in D:\kamlesh.patidar\project\php\twitter\twitter.php:229 Stack trace: #0 path_to_project_php\twitter\twitter.php(481): Twitter->doCall('statuses/friend...', Array, true, false) #1 \path_to_project_php\twitter\twitter\SendTwitter.php(13): Twitter->getFriendsTimeline() #2 {main} thrown in \path_to_project_php\twitter\twitter.php on line 229
What i can do?
Kamlesh
Tijs Verkoyen wrote:
15/08/2009
@Kamlesh Patidar: As you can see in the Exception-message, the connection couldn't be established. Probably because the Twitter was down, or you are working behind a firewall.
@David Jones: The class doesn't implement the Twitter Search Webservice methods. You should take a look at http://apiwiki.twitter.com/Twitter-Search-API-Method:-search and http://php.net/curl. That should get you started.
Devis wrote:
26/08/2009
Hi, great class, I use it to post phpbb topics. Sometimes I get a Timeout Exception on Curl, even if the timeout is set 60 secs, the timeout occurs after few seconds. Any I can live with it.
Thanks!
Devis wrote:
26/08/2009
Hi, great class, I use it to post phpbb topics. Sometimes I get a Timeout Exception on Curl, even if the timeout is set 60 secs, the timeout occurs after few seconds. Any I can live with it.
Thanks!
Tijs Verkoyen wrote:
27/08/2009
@Devis: Can't do anything about it. A timeout means the connection to Twitter can't be established. So it means Twitter is down or the connection is interupted.
Amir Syafrudin wrote:
28/08/2009
Sorry for the monologue. The problem was indeed because I'm sitting behind a proxy. I simply set the appropriate options in the doCall functions and I'm set to go.
Thanks.
sp_ wrote:
02/09/2009
Thanks, your class is the first out of the four php-twitter classes I've found so far.
Alexander Fleites wrote:
02/09/2009
I can't download any class because
all links are broken.....
Mmmmmmmmmmmmmm.
Tijs wrote:
02/09/2009
@Alexander Links are working here. Could you point the page with broken links?
jaris wrote:
07/09/2009
Thanks for the script. How to get the user profile image?
best regards
Chriske wrote:
16/09/2009
Dag Tijs, ik wil de classe installeren op mijn site. Staat ergens beschreven wat je juist moet aanvullen in het bestand twitter.php
Thx
davide wrote:
18/09/2009
nice job, easy to understand and well documented. i use it on my personal site. here my comment about your class:
http://sitoz.org/blog?p=408#comment-2640
PrintSpeed wrote:
19/09/2009
Hi, this script looks great. I think i will use it at my website.
Thank you.
Emmanuel wrote:
30/09/2009
Hello,
There is a bug into line 171, when i have one text with a lot of words with accents like
"olá está é uma mensagem ação ações"
And, the update status doesnt work very well, removing parts of the text.
I suggest update your script into line 171 to:
foreach($aParameters as $key => $value) $var .= '
CNSKnight wrote:
02/10/2009
lol - thought you had a spam-buster class too! nuh?!
(nntr)
Joel wrote:
02/10/2009
Hi, I'd love to have some sort of 'caching' system with this too. If you have a website that gets hit a ton, it would be great to see if you can just pull the cached version, or go get the new statuses.
Tijs wrote:
02/10/2009
@jaris: use getUser
@Chriske: you don't have to fill in anything in the class itself. See documentation for the usage.
@CNSKnight: indeed, but the wrapper class doesn't block the spam, it communicates with Akismet. So blame Akismet.
@Joel: you should build the caching yourself. I think it is bad practice to get the data each hit. You should a scheduled task (aka cronjob) to get the data.
Sam wrote:
08/10/2009
Nice work indeed!
Your documentation is great, I hope it serves as an inspiration to developers everywhere.
All the best,
Blog trackback is here: http://boolean.co.nz/blog/p=184
Rafael Vale wrote:
09/10/2009
Hi there. Thanks for the nice class!
I just can't understand how to display the 'time' of the message. i dunno what's this id in 'created_at'... Can you help me?
Tijs wrote:
09/10/2009
@Sam: thx
@Rafael: it is a unix_timestamp, you can format it with the date-function (http://php.net/date)
Christoph wrote:
15/10/2009
Hi Tijs,
thank you for you nice PHP Twitter class and the very very good documentation.
I tried to check the previous comments to see if someone had the same problem like me, but I have not found a similar problem.
If I try to use the follow()-function (calling $twitter->follow("xxxx") )with the follower-id as a string I alway get the error:
Fatal error: Uncaught exception 'TwitterException' with message 'There was a problem following the specified user.' in [...]/twitter.php:200 Stack trace: #0 [...]/twitter.php(1550): Twitter->doCall('notifications/f...', Array, true) #1 [...](71): Twitter->follow(XXXXXXXX) #2 {main} thrown in [...]twitter.php on line 200
(I replaced the filepath by [...] to keep the post short and replaced the id by X.)
Is it a problem of your class or is it my mistake?
Thank you for your help.
Christoph
Ulysses wrote:
16/10/2009
Hi Tijs, First of all, thank you for the great class. This is one of the easiest Twitter class I've work with. I've tried several PHP Twitter classes. I settled with yours. I've implemented your class on my site with the ability to show user, friends and public messages. I can also update statuses, (un)favorite, (un)follow, etc. It works great. Occasionally, I get an error message that Twitter is busy or I reached the 150 per hour limit :). How do I suppress the error messages? I want to show a user-friendly error message instead. Thanks!
Tijs wrote:
16/10/2009
@Christophe: I answered your question through email. But for other people with the same error: Before following a user you should be friends.
@Ulysses: use the try catch-structure (http://www.php.net/manual/en/language.exceptions.php).
Ulysses wrote:
17/10/2009
Tijs, Thanks for pointing me in the right direction. I learned something new today with regards to PHP exception handling. The concept is simple enough though. Just try, throw and catch. Thanks so much!
Leon wrote:
17/10/2009
Can someone tell me what is counted as part of the query length in the Twitter API? The documentation is really poor in this regard. And the Advanced Search tool doesn't seem to help in this determination:
http://search.twitter.com/advanced
What us being counted as part of the length of the query - entire
query string? What names (of query string name/value pair), values
(of query string name/value pair), and delimiters are counted in the
Twitter API restriction "Queries are limited 140 URL encoded
characters?" I've run the Twitter API Advanced Search Form to generate
queries:
http://search.twitter.com/advanced
At some point it will say the query is too large - should be 140 but
is 155
Query example:
http://search.twitter.com/search?q=
Henry Dawson wrote:
21/10/2009
Great class, just noticed a typo in the function name. verifyCrendentials should be verifyCredentials.
developar wrote:
22/10/2009
Thanks, I am using this class in many projects.
Just I have one question:
I saw in TODO that you are planning to implement OAuth into the class, so when you are planning to do it?
Thanks again
bill wrote:
24/10/2009
Hi,
when only writing this:
$twi_user = new Twitter("user","pass");
$user_text = "the above auth info is fake";
$twi_user->updateStatus($user_text);
I get the following:
Warning: curl_setopt_array() [function.curl-setopt-array]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/.../twitter.php on line 185
/statuses/update.xml Could not authenticate you.
Fatal error: Uncaught exception 'TwitterException' with message 'Unauthorized' in /home/.../twitter.php:225 Stack trace: #0 /home/.../twitter.php(646): Twitter->doCall('statuses/update...', Array, true) #1 /home/.../twitterauth.php(10): Twitter->updateStatus('the above auth info is fake') #2 {main} thrown in /home/.../twitter.php on line 225
Thanks for any help with this!
Tijs wrote:
24/10/2009
@bill: I think the warning says it all. Disable safe_mode.
Manoj wrote:
11/11/2009
Hi somehow i get this message:
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/.../.../.../twitter.php on line 52
maybe somebody could helP
Joe wrote:
15/11/2009
Hi, I'm trying to test the twitter bot and I created test file called twitter-11.php contain this :
<?php
require_once 'TwitterBot.class.php';
$bot = new TwitterBot('yayayaya', '123456');
$bot->searchAndRetweet('test', 'RT @%s: %s', true);
?>
This is the error I've got
Fatal error: Call to private method Twitter::getUsername() from context 'TwitterBot' in /home/gfffg/public_html/twitter/gghhgf/TwitterBot.class.php on line 71
Any ideas?
=======================
Can someone please delete those spam comments!!!!!
Matt Squirrell wrote:
23/11/2009
Hello there. I am using a version of PHP (5.04) that doesn't support curl_setopt_array. I replaced line 185 with the following:
foreach ($options as $k=>$v) {
curl_setopt($curl, $k, $v);
}
and all was fine.
Henry Pena wrote:
10/12/2009
Hello,
Do you know when you'll get around to updating the class to include the lists apis?
Thank you!
Best Regards,
~Henry~
Francis David wrote:
10/12/2009
I have problems with accents when i send a status update.
if i send: á123 é123 Ã123 is posted in twiter á3 é3 Ã3.
anyone can help me?
Thanks!
Francis David wrote:
10/12/2009
Now it´s working...
change the line 171
urlencode(utf8_encode($value));
Jelle De Loecker wrote:
13/12/2009
Hi,
I'm using your php class for a Facebook twitter-syncing app.
I have a strange experience with getFriendsTimeline():
$timeline = ($twitter->getFriendsTimeline(null,null, 200, 1));
As you can see I requested 200 tweets, but it only gives me about 187 tweets. When I ask for 180 tweets it only gives me about 160 tweets.
Do you know what could be the cause of this?
Thanks!
Francis David wrote:
15/12/2009
I´d like to know if is posible to use reTwit in this class.
If is posible, how can i use?
Thank´s!
Andre wrote:
22/12/2009
Hi there. First things first: thanks for this class. Appreciate your work.
Now the problem :) I got a local xampp installation and everything works fine. In an online shared hosting environment (same curl lib, nearly same php version) I got the following error: " /statuses/user_timeline.xml This method requires authentication." on calling curl_exec($curl) in doCall(...) -> I use the getUserTimeline() method. Same code, same twitter account. Any idea?
TIA Andre
Andre wrote:
22/12/2009
Small addition: I tested my example on two other shared hosters - works perfectly. Don't know what's wrong with the first one.
Balu wrote:
05/01/2010
Hi Tijs,
do you know a solution to check, if a twitter username/password combination exists or check if it is valid?
Is there a hidden possibility in your class to do so, maybe with a return bool.
Greetings from Germany
Tijs wrote:
10/01/2010
@Balu:
There isn't a method to test that, but you could create a new Twitter-instance and call the verifyCredentials-method.
// init vars
$username = '<username-to-check>';
$password = '<pass-to-check>';
// create instance
$instance = new Twitter($username, $password);
$isValidCombination = $instance->verifyCredentials();
Tijs wrote:
10/01/2010
Changelog since 1.0.4
- renamed verifyCrendentials to verifyCredentials (typo)
Ozzypig wrote:
14/01/2010
Wow, nice API! It musta taken a while to do. I'll be installing this into my site so that users can Tweet what they are doing.
Mindcrime wrote:
14/01/2010
2 small bugs in 1.0.5:
getFriends(): $cursor should be $page
Also add in doCall before:
$options[CURLOPT_FOLLOWLOCATION] = true;
the following:
// CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
$open_basedir = ini_get('open_basedir');
$safe_mode = ini_get('safe_mode');
if (empty($open_basedir)
Mindcrime wrote:
14/01/2010
invalid
Mindcrime wrote:
14/01/2010
argh:
ampersand ampersand empty($safe_mode))
Mindcrime wrote:
14/01/2010
I also remove all utf8_decode() in the class, the class should keep UTF-8...
Adam wrote:
26/01/2010
Hi everyone . i came up with the retweet function :)
/**
* Retweets the supplied status id
*
* @return array
* @param int[optional] $id
*/
public function retweetStatus($id)
{
// redefine
$id = (string) $id;
// build url
$url = 'statuses/retweet/'. urlencode($id) .'.xml';
// build parameters
$aParameters = array();
$aParameters['id'] = $id;
// do the call
$response = $this->doCall($url, $aParameters, true);
// convert into xml-object
$xml = @simplexml_load_string($response);
// validate
if($xml == false) throw new TwitterException('invalid body');
// return
return (array) $this->statusXMLToArray($xml);
}
Daz wrote:
01/02/2010
Hi Tijs,
Great API. I installed and it works great for me :)
Quick question. I tried to get the setUserAgent method to work. Eg...
$t = new Twitter($username, $password);
$t->setUserAgent('someUserAgentName');
However, whenever I send tweets out it appears as:
"Sent from API"
I would ideally love it to say, "Sent from someUserAgent" or something similar with API in the text.
Any help you be much appreciated.
1mard wrote:
02/02/2010
hi
tanx for class.
I have a problem same Daz.
I setUserAgent but whenever I send tweets out it appears as:
"Sent from API"
1mard wrote:
04/02/2010
Hi Tijs
Have any solution my problems?I had already asked:
I setUserAgent but whenever I send tweets out it appears as:
"Sent from API"
Tijs Verkoyen wrote:
04/02/2010
@Daz, @1mard: The UserAgent isn't related to "Sent from xxx".
It seems like Twitter has changed their API. In the past you could use a from-parameter in their current API you have to implement oAuth (more info: http://apiwiki.twitter.com/FAQ#HowdoIget“fromMyAppâ€appendedtoupdatessentfrommyAPIapplication)