Zend Framework and the Twitter API
I wanted my new job website to post a tweet to twitter every time we approved a posting.
Zend_Service_Twitter looks like it will be fairly comprehensive, but it’s not in the core yet and is probably a little overkill for my simple use case.
I then had a look at Zend_Rest_Client, which seemed to confuse me. I couldn’t actually get it to add the parameters I wanted to the call, I guess it’s better for interacting with Zend_Rest_Server or fully restful APIs.
To be fair, the manual actually states:
[Warning] Strictness of Zend_Rest_Client
Any REST service that is strict about the arguments it receives will likely fail using Zend_Rest_Client, because of the behavior described above. This is not a common practice and should not cause problems.
So here’s some simple code using Zend_Http_Client.
< ?php
require_once 'Zend/Http/Client.php';
$http = new Zend_Http_Client('http://twitter.com/statuses/update.xml', array(
'maxredirects' => 0,
'timeout' => 10,
));
$http->setAuth(
'twitter_username',
'twitter_password',
Zend_Http_Client::AUTH_BASIC
);
$http->setMethod(Zend_Http_Client::POST);
$http->setParameterPost('status', 'Your status message');
$http->request();
?>
Tags: PHP, twitter, zend framework








October 13th, 2008 at 5:53 pm
Jon Whitcraft has taken the version currently in the incubator and greatly refactored it; you can find it on github at http://github.com/sidhighwind/phly_twitter/tree/master
I’d recommend giving it a spin. :)
October 15th, 2008 at 6:36 am
Java developers can use this component:
http://www.servletsuite.com/servlets/twittertag.htm
October 21st, 2008 at 4:04 pm
http://pear.php.net/Services_Twitter is quite complete and easy to use.
statuses->update(’New Message’);
?>
Done. :)
October 21st, 2008 at 5:22 pm
looking forward for more information about this. thanks for sharing. Eugene
October 22nd, 2008 at 5:49 am
Agh! Never parallel the terms Zend_Rest_Server and “fully restful api’s” ;)
October 22nd, 2008 at 8:32 am
No curl or libs really needed, smth like this should work:
$context = stream_context_create(array(’http’ => array(
‘method’ => ‘POST’,
‘header’ =>
“Content-type: application/x-www-form-urlencoded\r\n” .
‘Authorization: Basic ‘ . base64_encode(’username:password’),
‘content’ => http_build_query(array(’status’ => ‘your message’)),
)));
var_dump(file_get_contents(’http://twitter.com/statuses/update.xml’, false, $context));
October 22nd, 2008 at 8:43 am
@Matthew: Cheers, will check it out.
@David: Cheers, will check it out. I need to start actually going to the pear website to check for libraries, I depend on google finding things for me too much.
@Luke: I hear ya buddy!
@NoDeps: Doesn’t look as nice though does it ;)
October 22nd, 2008 at 11:02 am
[...] ????? ??????? ???????????? ??????? ??? ????????? Zend Framework ? Twitter API. ? ??? ????????? ??????? ? ??????? ?????? ???????? [...]
October 22nd, 2008 at 11:22 am
[...] Marshall has a (very) quick post on tying together your Zend Framework application with the Twitter [...]
October 29th, 2008 at 5:29 pm
[...] Zend framework and the twitter api - Dave Development [...]
October 30th, 2008 at 3:49 am
I have updated the code tonight with more unit tests. I’m still working on it but we are shoting to have it in the incubator by Monday.
November 1st, 2008 at 12:18 pm
[...] Peses a que hay gente que la está usando con éxito, otros prefieren usar el Zend_Http_Client [...]