Twitter for PHP
Twitter for PHP is a very small and easy-to-use library for sending messages to Twitter and receiving status updates with OAuth support.
It requires PHP (version 5 or newer) and is licensed under the New BSD License. You can obtain the latest development version from our GitHub repository.
Getting started
Create object using OAuth keys:
$twitter = new Twitter($consumerKey, $consumerSecret,
$accessToken, $accessTokenSecret);
The send() method sends new tweet. The message must be encoded
in UTF-8 and it automatically makes long URLs short using is.gd service.
$twitter->send('I am fine today.');
The load() method returns the 20 most recent statuses posted in
the last 24 hours by you:
$channel = $twitter->load(Twitter::ME);
or posted by you and your friends:
$channel = $twitter->load(Twitter::ME_AND_FRIENDS);
or most recent mentions for you:
$channel = $twitter->load(Twitter::REPLIES);
Displaying tweets
The returned channel is a SimpleXMLElement object. Extracting the information from the channel is easy:
<ul>
<?php foreach ($channel->status as $status): ?>
<li><a href="http://twitter.com/<?= $status->user->screen_name ?>">
<?= htmlspecialchars($status->user->name) ?></a>:
<?= Twitter::clickable($status->text) ?>
<small>at <?= date("j.n.Y H:m", strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
</ul>
The static method Twitter::clickable() makes links in status
clickable. In addition to regular links, it links @username to the
user’s Twitter profile page and links hashtags to a Twitter search on that
hashtag.
Searching
The search() method provides searching in twitter statuses:
$twitter = new Twitter;
$results = $twitter->search('#nette');
The returned result is a PHP array:
<?php foreach ($results as $result): ?>
<li><a href="http://twitter.com/<?= $result->from_user ?>">
<img src="<?= htmlspecialchars($result->profile_image_url) ?>" width="48">
<?= htmlspecialchars($result->from_user) ?></a>:
<?= Twitter::clickable($result->text) ?>
<small>at <?= date("j.n.Y H:i", strtotime($result->created_at)) ?></small>
</li>
<?php endforeach ?>
Error handling
All methods throw a TwitterException on error:
try {
$channel = $twitter->load(Twitter::ME);
} catch (TwitterException $e) {
echo "Error: ", $e->getMessage();
}
Additional features
The authenticate() method tests if user credentials
are valid:
if (!$twitter->authenticate()) {
die('Invalid name or password');
}
Komentáře
» přidat
Tento článek byl uzavřen. Už není možné k němu přidávat komentáře.

#1 veena http://dvanula.cz nový
Díky Davide, zrovna Twitter chceme použít v jednom novém „omračujícím
“ projektu. Tak jsi nám to
jenom ulehčil!
Díky, pokud se projekt povede, pozvu tě na pivo ;-D
#2 Inza http://www.dvojmo.cz nový
#1 veena: Tak to nás bude víc, co dělají na “omračujících” projektech a pozvou tě na pivo:-)
#3 Marcus Povey http://www.marcus-povey.co.uk nový
Twitter broke/fixed their API recently and now this library returns 417 when attempting to post.
Fix: Add the following line to twitter.class.php:113 just after the other curl_setopt lines and be fore the if ($post)…
curl_setopt($curl, CURLOPT_HTTPHEADER, array(‘Expect:’));
#4 David Grudl http://davidgrudl.com nový
#3 Marcus Povey: thank you Marcus, I have updated it.
#5 Martin Ragg http://www.reticon.de nový
I've s.th. “strange” – I'm using your class and normally all is working fine – today I twittered “Königinnen der Nacht” and it seems to me that the class “killed” some letters. On twitter always there was only “Köinnen der Nacht” – do you have an idea?
(Directly from twitter all was working fine .. )
best regards,
martin
#6 David Grudl http://davidgrudl.com nový
#5 Martin Ragg: And was the message encoded in UTF-8?
#7 Martin Ragg http://www.reticon.de nový
#10 David Grudl – Oh sorry, my fault – there was no utf8_encode, I fixed my script and it seems to be working finde
#8 Ryan Taylor nový
Hello David. Thank you very much for writing this PHP class for Twitter. I have one question: is there anyway to return the status ID of a tweet using the send() method? Currently it looks as though you have it returning only a boolean true/false.
Thanks again!
#9 Robb Corrigan http://twitcharts.com nový
Hello David, excellent API! I'm using this in a simple hobby project that I've built which allows people to tweet Google charts. http://twitcharts.com/
I've given you a plug here: http://twitcharts.com/about.php
Thanks again!
#10 David Grudl http://davidgrudl.com nový
#8 Ryan Taylor: Ryan I have updated send() method, it returns ID on success and FALSE on failure now.
#11 Nathan nový
It seems that Twitter returns tweets in UTC time. Is there an easy way to make the time and date conform to my time zone?
I saw something that might help, here: http://www.alephnaught.com/Blog/?… but my php knowledge is too limited to implement it with your solution.
Thanks,
Nathan
#12 Dragon Jake http://blog.dragonjake.net nový
Díky Davide za aktualizaci! Už jsem měl plány, že podporu ve svém forku nějak zkompletním (třeba odstraním zbytečnou druhou knihovnu a zlepším celý proces autorizace, jak jsi udělal Ty) a nenechám ji v tomhle stavu “nějak to funguje na to, co potřebuju”. Jsem rád, že je mohu zapomenout a věnovat se jiným věcem ;]
#13 srigi nový
Hello David, thank for good job. But I got problem, maybe I'm just blind or stupid, but how do I get
$accessTokenand$accessTokenSecret. I think that sctript must run OAuth loop (my app, twitter, return to my app), but this isn't covered in this article. Can you please add short desctiption/tutorial on this?THX.
#14 David Grudl http://davidgrudl.com nový
#13 srigi: RTFM
There is a readme.txt file with complete
explanation.
#15 Fost nový
Hi David,
Very nice! Just one question: Is there a way of changing the limit from 24 hours for load()?
Thanks!
#16 Onecar http://tomaskrchnak.cz nový
Hello,
is there any way how to make links in
$status->textactive, so I can click right away? So far I only have almost plain text, only author is a link.Thanks for your response.
#17 David Grudl http://davidgrudl.com nový
#16 Onecar: in last revision there is a method Twitter::clickable().
#18 martin131 http://martin.matuna.cz nový
Ahoj, vrací se v poli i přímý odkaz na tweet? Plánuješ případně tuto možnost přidat??
#19 David Grudl http://davidgrudl.com nový
Ten se dá poskládat, viz příklady v článku.
#20 martin131 http://martin.matuna.cz nový
#19 David Grudl: Jsem asi slepý, ani po druhém dlouhém zkoumání jsem to v těch příkladech nenašel. :D Nakonec jsem se podíval do wiki Twitter API.