Skip to content
CakePHP 1.3+ plugin to interact with a Twitter account, now with OAuth
Pull request Compare This branch is even with pefringant:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Twitter plugin for CakePHP 1.3+ with OAuth

CakePHP plugin to interact with a Twitter account. For example, it allows you to post a new status on Twitter after a Model::save(), on create, update, or both. Now connect via OAuth as from June 30, 2010 the basic authentification on the api will be forbidden.

1. Installation

To install, run the following command from your APP/plugins folder:

git clone git:// twitter

The plugin includes a new Twitter DataSource based on the great work from Michael “MiDri” Riddle, Daniel Hofstetter and Alexandru Ciobanu.

2. Setup

2.1. Twitter account credentials

Go to, sign in and click “register an app”. Now define the app parameters, mainly:

  • Application Type: Browser
  • Default Access type: Read & Write

Once the app is registered, duplicate the following file:


and rename it to ‘twitter.php’. Open it and fill the credentials with yours:

	var $twitter = array(
		'datasource'         => 'twitter_oauth',
		'consumer_key'       => '',
		'consumer_secret'    => '',
		'oauth_token'        => '',
		'oauth_token_secret' => ''

The consumer key and secret can be found on the page “Application details”, and the oauth_token and oauth_token_secret can be found on the page “My Access Token”, linked on the right panel.

2.2. Attach to Model

The plugin includes a Behavior that can be attached to any Model. The only configuration needed is when do you want to post a new status on Twitter : on create, on update, or both.

class Post extends AppModel {
	var $actsAs = array('Twitter.Twitterable' => array(
		'on' => 'create'

In this example, everytime a Post is created, a new status will be posted on our Twitter account.

2.3. Create a Model::twitterStatus() method

The Twitterable Behavior will look for a twitterStatus() method in the attached Model. This method must return the status to be posted. Keep in mind that a status on Twitter is limited to 140 characters.

The plugin includes a convenient method to format a status, twitterFormatStatus(). This method takes 3 parameters:

  • $message: required text of the status ;
  • $url: optionnal url, to the full post for example. Will be shortened (we choosed the free url shortener service ;
  • $ending: optionnal ending string if the status text is too long. Defaults to ‘…’.

Full example:

class Post extends AppModel {
	var $actsAs = array('Twitter.Twitterable' => array(
		'on' => 'create'
	function twitterStatus() {
		$title = $this->data['Post']['title'];
		$url   = Router::url(array('controller' => 'posts', 'action' => 'view', $this->id), true);
		return $this->twitterFormatStatus($title, $url, '...');

The status will be the post’s title (cut and ended by ‘…’ if too long), followed by a space and the url in a shortened format.

3. Other Twitter interactions

Since we are using a full featured Twitter DataSource, we can access all the other Twitter API methods. Please refer to the Twitter API to know the available methods.

Note that we don’t need the plugin’s Behavior to access the DataSource methods, we only need to call the Twitter DataSource from the plugin.

Example: we look for ‘cakephp’ on Twitter and display the results :

class SomethingsController extends AppController {
	function index() {
		$ds = ConnectionManager::getDataSource('twitter');
		$results = $ds->search(array('q' => 'cakephp');
Something went wrong with that request. Please try again.