Skip to content

pefringant/twitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twitter plugin for CakePHP

CakePHP plugin to interact with your Twitter account (see the Twitter API for reference). For example, it allows you to post a new status on Twitter after a Model::save(), on create, update, or both.

1. Installation

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

git clone git://github.com/kalt/twitter.git

The plugin includes the Twitter DataSource for CakePHP by Alex Ciobanu, added as a git submodule. You have to update it:

git submodule init
git submodule update

2. Setup

2.1. Twitter account credentials

Duplicate the following file:

twitter/config/twitter.default

and rename it to ‘twitter.php’. Now open it and replace the username and password with yours.

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 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 your Twitter account.

2.3. Create a Model::twitterStatus() method

The Twitterable Behavior will look for a twitterStatus() method in your model. This method just have to return the status you want to post. Keep in mind that a status on Twitter is limited to 140 characters.

We included a convenient method to format a status, twitterFormatStatus(). This method takes 3 arguments:

  • $message: required text of the status ;
  • $url: optionnal url, to the full post for example. Will be shortened (we choosed http://is.gd) ;
  • $ending: optionnal ending string if the status 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 the Twitter DataSource, we can access various other Twitter API methods. Please read the Bakery article about the Twitter DataSource to see what you can do.

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

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

class SomethingsController extends AppController
{
	var $uses = array('Something', 'Twitter.Twitter');

	function index()
	{
		$ds = ConnectionManager::getDataSource('twitter');
	
		$test = $ds->search('cakephp');
        
		debug($test);
	}
}

About

Twitter plugin for CakePHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages