301 Moved Permanently

Image Image Image 01 Image Image Image Image Image Image Image Image Image Image

My Simple Online Portfolio.

Hi, this is Md. Asef Jamal (Ratul),echo from Bangladesh. Define myself as a Web Programmer/Developer and currently looping through Aan-nahl for 2 years.

© Copyright 2012
Asef Ratul's Portfolio

Scroll to Top

To Top


How to post tweet to your twitter account?

Sometimes programmer needs to post tweet from PHP script. There are lots of plugin for this, but you can post your tweet from the script too very easily. For this you need to download the tweet OAuth Library for PHP. You can get the library from twitter developer page or you can directly download it from here. After that you will need 4 keys for tweet post. You can get the keys from twitter developer page by creating a simple developer APP. Please go this link and create your first APP. After creating an APP you will get 4 API keys: Consumer_key, Consumer_secret, Access_token and Access_token_secret.

Set the API keys.

define(‘TW_CONSUMER_KEY’, ‘consumerkey’);

define(‘TW_CONSUMER_SECRET’, ‘consumersecret’);

define(‘TW_ACCESS_TOKEN’, ‘accesstoken’);

define(‘TW_ACCESS_TOKEN_SECRET’, ‘accesstokensecret’);

function tweet_post($tweet) {

if( strlen($tweet) > 140 ) return ‘Sorry, tweet can not be posted. Tweet character length excced its limit.’;  // check for maximum tweet character

require_once(‘twitteroauth/twitteroauth.php’);  // add the tweetOAuth Library

$hasPost = true;

$tweetOauth = new TwitterOAuth(TW_CONSUMER_KEY, TW_CONSUMER_SECRET, TW_ACCESS_TOKEN, TW_ACCESS_TOKEN_SECRET);   // connect with the OAuth

$tweetOauth->oAuthRequest( ‘https://api.twitter.com/1/statuses/update.json’, ‘POST’, array(‘status’ => $tweet) );   // request tweet post with the OAuth connection

if( $tweetOauth->http_code != 200 ) {  // Check if has some error

$hasPost = false;


return $hasPost;


tweet_post( ‘This is my first tweet post from PHP script’ );   // call the function

First we define our API keys. Then we create a function named “tweet_post“. First we check the tweet character length. In twitter you can post maximum 140 character. So we have to check the tweet message. Next, we are included the OAuth Library. Then make the OAuth connection and post your tweet message. If “http_code” returns 200, then your tweet is successfully submitted to your account. Otherwise has some errors.

Sometimes you will need to use url shortener. You can use the following function to short your url link.

function get_short_link($url) {
$api_call =  file_get_contents(“http://api.bit.ly/shorten?version=2.0.1&  longUrl=”.$url.”&login=”.$bitly_login.”&apiKey=”.$bitly_apikey);
if ($bitlyinfo[‘errorCode’]==0) {
return $bitlyinfo[‘results’][urldecode($url)][‘shortUrl’];
} else {
return 0;

Please go to the bit.ly to create an account and get the api key for url shortener. So, go on and make your first tweet post from PHP script.

Submit a Comment