Sunday, 28 March 2010

Twitter Integration

I've just added Twitter support to LRPevents.com.

Now, every time an item is added to the database, a notification is sent to the LRPevents Twitter account (@LRPevents).

Integration was very straightforward. I dropped Twitter Class into my /app/vendors directory and added the following to AppModel:


function afterSave($created) {

if ($created) {

App::import("Vendor", "twitter");

$twitterUser = Configure::read("Twitter.user");

if ($twitterUser) {

$twitter = new Twitter($twitterUser, Configure::read("Twitter.password"));

$message = "New " . $this->name . ": " . $this->data[$this->name][$this->displayField] . " " . Router::url(array("action"=>"view", $this->id), true); 
$result = $twitter->updateStatus($message);

}

}

}

And put the twitter credentials in /app/config/bootstrap.php:

Configure:write("Twitter.user", "{your twitter username}");
Configure:write("Twitter.password", "{your twitter password}");

Easy eh?

Note: After going live with this feature I found that Twitter Class uses a setting that isn't allowed when your site is running in safe mode. The fix was easy: Just comment out the line


$options[CURLOPT_FOLLOWLOCATION] = true;

in twitter.php


No comments:

Post a Comment