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,, every new link published is automatically added to Twitter as a status at

The details of Twitter API are available at Basically, you need to register your app and use that info to login into Twitter and do stuff. The API is well documented at, 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.


include_once 'CurlWrap.php';

class Twitter
   private $curl;
   function __construct()
      $this->curl=new CurlWrap();
   function addStatus($msg)
      $this->curl->setOption(CURLOPT_POSTFIELDS, "status=".urlencode($msg));
      return ($http_code=='200'||$http_code=='201');



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();
if (!$shortblogurl) {
   print "ERROR: Problem making short url.\n";

$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";

This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s