Categories
PHP Twitter

How to Tweet from PHP and short Url with bit.ly

Since I released the version 1.4.1 of OC, 2 persons already ask me how to do the twitter post in their site. I used two simple functions that I found on internet and I modified them to make them work as my purpose.

The idea is to post any submit data into a twitter account and short the given url with bit.ly if it’s set.

First we need some defines (they can also be post form a form, for example):

//Twitter account
define('TWITTER','twitter_user');//user name for twitter
define('TWITTER_PWD','yourpassword');//password for twitter account
//Bit.ly config for url shortener, optional
define('BIT_USER', 'bit_user');//bit.ly user
define('BIT_API','bit_api_key');//bit.ly apikey

Then first we have the function to short the url’s with bit.ly (modified from james.cridland.net):

function get_bitly_url($url) {//short an url with bit
	if (BIT_USER!="" && BIT_API!=""){
		$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".BIT_USER."&apiKey=".BIT_API);
		$bitlyinfo=json_decode(utf8_encode($api_call),true);
		if ($bitlyinfo['errorCode']==0) {
			return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
		}
		else return $url;
	}
	else return $url;
}

To use it we call

get_bitly_url("http://oneurl.com/");

and returns the url if was able to do it or the original url in other case.

Then we use this function to post on twitter (modified from morethanseven.net):

function post_to_twitter($message,$link){ //post to twitter
	if (TWITTER!="" && TWITTER_PWD!=""){//only if the username password are set
		// Set username and password
		$username = TWITTER;
		$password = TWITTER_PWD;
		// The message you want to send, control size and create link
		$msg_size=140;//max size for twitt msg
		$link=get_bitly_url($link);//make the link shorter if it's set
		$msg_size-=(strlen($link)+1);//size that we have for the message after the link inserted +1 to leave an space between
		if(strlen($message)>$msg_size) $message=substr($message, 0, $msg_size);//crop the message then fits the url
		$message.=" ".$link;//message with the link

		// The twitter API address
		$twitterurl = 'http://twitter.com/statuses/update.xml';
		// Set up and execute the curl process
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, "$twitterurl");
		curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_handle, CURLOPT_POST, 1);
		curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
		curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
		$buffer = curl_exec($curl_handle);
		$resultArray = curl_getinfo($curl_handle);
		curl_close($curl_handle);
		// check for success or failure
	 	if($resultArray['http_code'] == "200")  return true;//success
		else return false;//twitter failure
	}
	else return false;
}

To use it simple:

post_to_twitter("here is a message","http://anotherlink.com");

and returns true if was success or false.

Also in the code we check that the url+message is not more than 140 chars and in case it is we take characters from the message.

Enjoy 😉

UPDATE: There was a mistake with the return of the post_to_twitter function, now works perfect.