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

?>

Twitter Icon If you liked this post, you should follow me on twitter here
blog comments powered by Disqus

About

Photo of Dave Marshall

Dave Marshall is a Software Engineer living near Hull, England. He works on various personal projects and is the Technical Manager at Childcare.co.uk

Dave specialises in web application development for the LAMP stack, but always tries to choose a tool set that is most fit for purpose.

Dave is a Zend Certified Engineer and a Member of the British Computer Soceity.

Read more about Dave

Follow Dave: