Posting your website updates to twitter using Zend Framework
Tweeting your website updates can be done in 4 simple steps
Register your application
Since twitter’s API change in mid-2010-ish you’re required to use oAuth to gain access to a remote twitter account.
To get the required tokens you’ll need to register what twitters calls an application.
Go to dev.twitter.com and sign in with the account you want to post your tweets to.
Once you’ve signed in click on “Create an app”.

Fill out all the required fields.
You don’t need a callback URL as you’re the only person who’s going to be using the application.
When you’ve completed the registration you’ll be forwarded to the details page of your application.
Click on the ‘Settings’ tab, change the application type to ‘Read and Write’ and press the button ‘Update this Twitter application’s settings’ on the bottom of the page.

Head back over to the ‘Details’ tab.
On the bottom of the screen you’ll see the box as shown below.

Click on the button ‘Create my access token’.
You’ll now see access tokens.
You’ll need 4 keys in total
- Consumer key
- Consumer secret
- Access token
- Access token secret
Implement an URL shortener
As the number of characters of a tweet is limited, it’s best to shorten your URLs.
Zend Framework provides wrappers for the following 4
The obvious pick would be is.gd as this is the shortest URL.
$oService = new Zend_Service_ShortUrl_IsGd(); $sShortUrl = $oService->shorten('http://www.madclog.nl'); // http://is.gd/VJS6hU
Prepare your tweet
As mentioned before the number of characters is limited, we’ll need to cap longer strings
Let’s take the following text as an example:
Hello world. This tweet was generated from my website. It should not be more then 140 characters long to please twitter. We might need to shorten this text.
That string on it’s own is 156 characters long and doesn’t even include the URL yet.
With a bit of math we can reduce the length and chop of the rest.
$sShortUrl = 'http://is.gd/VJS6hU'; $sUrlSeparator = ' - '; $sCapSuffix = '...'; $sTweet = 'Hello world. This tweet was generated from my website. It should not be more then 140 characters long to please twitter. We might need to shorten this text if that is the case.'; if (strlen($sTweet) > (140 - (strlen($sShortUrl) + strlen($sUrlSeparator)))) { $sTweet = substr($sTweet, 0, (140 - (strlen($sShortUrl) + strlen($sUrlSeparator) + strlen($sCapSuffix))) ).$sCapSuffix; } $sTweet .= $sUrlSeparator.$sShortUrl;
This would result in the following tweet
Hello world. This tweet was generated from my website. It should not be more then 140 characters long to please twi… – http://is.gd/VJS6hU
Tweet your update
Zend Framework has a wrapper for the Twitter API called Zend_Service_Twitter.
$sTweet = 'Hello world. This tweet was generated from my website. It should not be more then 140 characters long to please twi... - http://is.gd/VJS6hU'; $oToken = new Zend_Oauth_Token_Access(); $oToken->setToken('Access token')->setTokenSecret('Access token secret'); $oTwitter = new Zend_Service_Twitter(array( 'accessToken' => $oToken, 'consumerKey' => 'Consumer key', 'consumerSecret' => 'Consumer secret' )); $oTwitter->statusUpdate($sTweet);
Run the script, check your twitter account, be amazed!