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.
To install, run the following command from your app/plugins folder:
git clone git://github.com/kalt/twitter.gitThe 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 updateDuplicate the following file:
twitter/config/twitter.defaultand rename it to ‘twitter.php’. Now open it and replace the username and password with yours.
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.
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.
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);
}
}