Integrating apps with Twitter

Adding status to your Twitter account is so easy these days, and you might wonder what is the big deal about it. But, there could be some need to post a status or a piece of information from another application. In that case, the simple to use Twitter API come in handy; it can be used to integrate your application with Twitter in various ways: the most common being just posting a status to Twitter. For example, in the from the links aggregation site I developed, urlsreview.com, every new link published is automatically added to Twitter as a status at http://twitter.com/urlsreview.

The details of Twitter API are available at http://dev.twitter.com. Basically, you need to register your app and use that info to login into Twitter and do stuff. The API is well documented at http://apiwiki.twitter.com, with sample code.

It is extremely easy to add status to Twitter via API as it shown in the following example. Basically, the curl call must be an HTTP POST and the twitter username and password can be specified as curl options. With utility classes available for curl and shortening url, the code will look much simpler. The wrapper class for Twitter API that is defined here is only good for posting a new message; and it can be expanded to inlude all the available API’s.

Twitter.php

<?php
include_once 'CurlWrap.php';

define('TWITTER_API_STATUS_UPDATE','http://twitter.com/statuses/update.json');
define('TWITTER_USERNAME','YOUR-USER-NAME');
define('TWITTER_PASSWORD','YOUR-USER-PASSWORD');
   
class Twitter
{
   private $curl;
   
   function __construct()
   {
      $this->curl=new CurlWrap();
      $this->curl->setOption(CURLOPT_POST,true);
      $this->curl->setOption(CURLOPT_USERPWD, TWITTER_USERNAME.':'.TWITTER_PASSWORD);
   }
   
   function addStatus($msg)
   {
      $this->curl->setOption(CURLOPT_POSTFIELDS, "status=".urlencode($msg));
      $this->curl->exec(TWITTER_API_STATUS_UPDATE);
      $http_code=$this->curl->getHttpCode();
      
      return ($http_code=='200'||$http_code=='201');
   }
}

?>

add_twitter_status.php

<?php
   
include_once 'Twitter.php';
include_once 'ShortUrl.php';

//Let me post a message about one of my posts here to Twitter after shortening the url
$shorturl=new ShortUrl();
$shortblogurl=$shorturl->shortenUrl('https://smoothops.wordpress.com/2010/07/16/using-curl-to-make-rest-api-calls/');
if (!$shortblogurl) {
   print "ERROR: Problem making short url.\n";
   exit;
}

$msg="Posted an article on how to use curl in PHP. Check out $shortblogurl";

$twitter=new Twitter();
if ($twitter->addStatus($msg)) print "Status '$msg' has been added to Twitter.\n";
else "ERROR: Problem adding status '$msg' to Twitter.\n";

exit;
Advertisements
This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s