Cognitionis
Hector Llorens Portfolio

Twitter for developers


Twitter offers to developers access to their API.

Accessing&Managing information is done through OAuth. Having access means you can for example tweet automatically in your wall/feed from a PHP script.

How you get access to Twitter API to manage your account?

You need to own a twitter app: http://dev.twitter.com/apps/new

Create it for your domain (e.g., http://www.cognitionis.com) and at least read&write.

You will get a Consumer ID number and a Consumer Secret number. To get the access_token and token_secret you should follow the links.

To build your PHP code to access twitter you should use one of the twitter API libraries: https://dev.twitter.com/docs/twitter-libraries

In this example we use the Abraham one. Creating a script to post is very easy.

<?php
$consumerKey = '<insert your consumer key';
$consumerSecret = '<insert your consumer secret>';
$oAuthToken = '<insert your access token>';
$oAuthSecret = '<insert your token secret>';

require_once($_SERVER['DOCUMENT_ROOT'].'/<insert path to twitteroauth>/twitteroauth.php');

// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);

//send a tweet
$tweet->post('statuses/update', array('status' => 'Hello World'));
?>

That’s it.