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();

?>
  • Digg
  • del.icio.us
  • NewsVine
  • Reddit
  • Furl
  • description
  • StumbleUpon
  • Technorati

Tags: , ,


12 Responses to “Zend Framework and the Twitter API”

  1. Matthew Weier O'Phinney Says:

    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. :)

  2. Den Says:

    Java developers can use this component:
    http://www.servletsuite.com/servlets/twittertag.htm

  3. David Coallier Says:

    http://pear.php.net/Services_Twitter is quite complete and easy to use.

    statuses->update(’New Message’);
    ?>

    Done. :)

  4. Eugene Says:

    looking forward for more information about this. thanks for sharing. Eugene

  5. luke Says:

    Agh! Never parallel the terms Zend_Rest_Server and “fully restful api’s” ;)

  6. NoDeps Says:

    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));

  7. daveyboy Says:

    @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 ;)

  8. ???????? ????????? ? Twitter | Zend Framework ??-?????? Says:

    [...] ????? ??????? ???????????? ??????? ??? ????????? Zend Framework ? Twitter API. ? ??? ????????? ??????? ? ??????? ?????? ???????? [...]

  9. Dave Marshall’s Blog: Zend Framework and the Twitter API : Dragonfly Networks Says:

    [...] Marshall has a (very) quick post on tying together your Zend Framework application with the Twitter [...]

  10. Links interesantes Says:

    [...] Zend framework and the twitter api - Dave Development [...]

  11. Jon Whitcraft Says:

    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.

  12. Batido de naranja - by victorcoder » Zend Framework y el API de Twitter Says:

    [...] Peses a que hay gente que la está usando con éxito,  otros prefieren usar el Zend_Http_Client [...]

Leave a Reply