Twitter for PHP
Twitter for PHP is a very small and easy-to-use library for sending messages to Twitter and receiving status updates.
It requires PHP (version 5 or better) with cURL extension and is licensed under the New BSD License.
Usage
Create object using your credentials (user name and password)
$twitter = new Twitter($userName, $password);
The send() method updates your status. The message must be encoded in UTF-8:
$twitter->send('I am fine today.');
The load() method returns the 20 most recent status updates 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);
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?>"><?=$status->user->name?></a>:
<?=$status->text?>
<small>at <?=date("j.n.Y H:m", strtotime($status->created_at))?></small>
</li>
<?php endforeach ?>
</ul>
The authenticate() method tests if user credentials are valid:
if (!$twitter->authenticate()) {
die('Invalid name or password');
}
The search() method provides searching in twitter statuses:
$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="<?=$result->profile_image_url?>" width="48"> <?=$result->from_user?></a>:
<?=$result->text?>
<small>at <?=date("j.n.Y H:i", strtotime($result->created_at))?></small>
</li>
<?php endforeach ?>
You can obtain the latest development version from our GIT repository.
(má angličtina je strašná, budu rád, když mě opravíte).
Komentáře
» přidat
Tento článek byl uzavřen. Už není možné k němu přidávat komentáře ani hlasovat

#1 Developer http://developer.xf.cz/blog nový
Nuže řekl sis o to.
Bude to asi dlouhý, klidně vymaž po opravě.
Twitter for PHP is a very small and easy-to-use library for sending messages to –the– Twitter and receiving statuses (lépe asi status updates).
Method send() updates your status. The message must be encoded in UTF-8: → The send() method updates your status. The message must be encoded in UTF-8.
Method load() returns the 20 most recent statuses posted in the last 24 hours from you and your friends (optionally). → The load() method returns the 20 most recent status updates posted in the last 24 hours by you and optionally by your friends.
The returned channel is a SimpleXMLElement object. Extracting the information–s– from the channel is easy:
Teď po náhledu jsem zjistil, že nevím jak se v Texy2 dělají přeškrtnutí (jsem zvyklý na jedničku), tak je to co má být umazáno označeno takhle –smaz me–.
#2 David Grudl http://davidgrudl.com nový
#1 Developer: díky moc!
#3 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
#4 Inza http://www.dvojmo.cz nový
#3 veena: Tak to nás bude víc, co dělají na “omračujících” projektech a pozvou tě na pivo:-)
#5 Snuggles http://hufman.us nový
Your date() command should use “j.n.Y H:i” instead of “j.n.Y H:m”. Thank you for the excellent code!
#6 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:’));
#7 David Grudl http://davidgrudl.com nový
#6 Marcus Povey: thank you Marcus, I have updated it.
#8 David Grudl http://davidgrudl.com nový
#5 Snuggles: thats right
#9 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
#10 David Grudl http://davidgrudl.com nový
#9 Martin Ragg: And was the message encoded in UTF-8?
#11 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
#12 Ryan Taylor rtaylor@mail.ru 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!
#13 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!
#14 David Grudl http://davidgrudl.com nový
#12 Ryan Taylor: Ryan I have updated send() method, it returns ID on success and FALSE on failure now.
#15 Nathan sungkhum@gmail.com 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