Tag Archives: Bit.ly

Expand any shortened url using PHP

Recently for a project which I was working on, I wanted a way to expand urls shorted by url shorteners.

After a couple of web searches I found an API site called LongURL. This service was really slow and I had to look for alternatives.

I thought of writing my own using curl and then parse the headers. I was searching for the curl functions in PHP Manual and I found a simple but not so famous function called get_headers().

This function is all we need to expand the urls. The following function expand_url() takes a short url and will return the longer version of the url.

Enjoy 🙂

Posted in Web Programming | Tagged , , | 11 Comments

Consolidated all my code in github

I got introduced to Git and Gitbub by Yuvi and ever since I have been hooked up. I have started using it for all my pet projects. I even convinced Steve Bruner so that I can use Github for RoloPress. 🙂

Over the years I have released lot of code out in the wild and I thought of consolidating all of them in Github, so that it can be of use to someone who might need them. At last I found some time and uploaded most of them to my Gitbub account.

Below is the description of some of those projects which are currently there in my Github account.

Bright Light

Bright Light is the WordPress theme, which is powering up my blog. I have released it out hoping that it might be useful for someone.

Android Samples

I created this project to share my homework and the sample code used in the “Developing Android Applications in Java” online class, which I am currently following.

RoloPress core and RoloPress Default

These projects contain my contribution to RoloPress, a WordPress based contact manager. You can read more about WordPress from its homepage.

FeedBurner – stats

FeedBurner-stats is a Google Gadget which allows you to keep track of your Feedburner subscriber count. It uses Google Chart API and Google Gadget API.

Bloglines Notifier

Bloglines Notifier is a Google Gadget which will notify you the count of unread items in your Bloglines account. (Hope someone is still using Bloglines 😉 )

Retweet

It is a fork of John Resig’s retweet script which I am using in my Easy Reweet WordPress Plugin. I have added a new feature to this script which allows you to associate the list of urls created to your bit.ly account.

Count Words

It’s a small Ruby script which prints the number of words (with their count) present in a given text file

Bulk unrar

It is a small Ruby script which unrars all files found in all the subdirectories of a director given in the command line.

I am still undecided about porting my WordPress Plugins from the official WordPress Plugin repository to Github. I guess it is better to have my Plugins in the Plugin repository for now.

Update (Feb-2013): I have now started to use github for my WordPress Plugins as well.

BTW feel free to fork any of these projects and I would be happy to pull in your changes if you have added some enhancements to them. 🙂

Posted in Random/Personal | Tagged , , , | 2 Comments

Associating urls created with bit.ly API to your account

Recently while working on adding the feature to enter your own bit.ly API key to my Easy Retweet WordPress Plugin, I found out that by default all short urls created using bit.ly API (both REST and JavaScript API’s) are not associated with your account.

Upon further researching I found that there is an undocumented way of associating the urls created with your account. The undocumented way is to add an additional parameter called history with value 1 to the API URL.

Using the history parameter in bit.ly’s REST API

So for REST API, you have to use the following url.

http://api.bit.ly/shorten?version=2.0.1&longUrl=http://sudarmuthu.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1

If you are using PHP, then code would be

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using curl
	$curlObject = curl_init();
	curl_setopt($curlObject,CURLOPT_URL,$url);
	curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($curlObject,CURLOPT_HEADER,false);

	$result_json = curl_exec($curlObject);
	curl_close($curlObject);

	//decode JSON. Assumes that it is PHP5
	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

If you are going to use it in WordPress, then you can use the inbuilt WP_Http class instead of curl as suggested by Ozh. The following code shows you how it can be done in WordPress

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using WP_Http present in WordPress
	$request = new WP_Http;
	$result_json = $request->request($url);

	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

Using the history parameter in bit.ly’s JavaScript API

If you are using bit.ly’s JavaScript API, then it is not as straight forward as the REST API. Instead of using the provided shorten method, you have to use the low level call method.

The following code shows you how you can do it in JavaScript API.

BitlyClient.call('shorten', {'longUrl':'http://sudarmuthu.com', 'history':'1'}, 'BitlyCB.shortenResponse');

I am not sure why bit.ly is not associating the created shorturls automatically with your account, when you provide the API Key, it is the expected default behavior. Or at least they could have documented about this history variable in their API. I guess only someone from bit.ly can answer this. 🙂

Posted in API's/Mashup, JavaScript/jQuery, WordPress | Tagged , , | 8 Comments

Adding retweet buttons for WordPress posts

One of the recent trends in the blogsphere is to display the retweet or “tweet this” button on your pages together with the number of times they were retweeted. There are lots of ways and services to do it and each of them have their own pros and cons.

Recently John Resig released a pure JavaScript solution for adding retweet buttons to your pages. It uses Bit.ly JavaScript API, has no dependencies and is totally unobtrusive.

Like my previous Geo Mark WordPress Plugin, I converted his excellent JavaScript solution into a WordPress Plugin called Easy Retweet, so that it can be used in WordPress without making many changes to the theme.

Plugin Usage

You can download the Plugin from the Plugin’s home page. There are two ways you can add the retweet button, the automatic way and the manual way.

Automatic way

Install the Plugin and choose the type and position of the button from the Plugin’s settings page.

Easy Retweet WordPress Plugni settings

Manual way

If you want more control over the way the button should be positioned, then you can manually call the button using the following code.

if (function_exists('easy_retweet_button')) {
	echo easy_retweet_button();
}

Feedback

Try out the Plugin and let me know your thoughts. The Plugin is still in the early stages of development and I will add more features based on the feedback.

Posted in Plugin Releases | Tagged , , , , | 8 Comments