Shorten your link using bitly API

Below is the code on how to make use of bitly API but first things first. In order to use the code below, you must create an account in bitly to gain your login and API Key which are both required in the API call. Once you have that setup, copy and paste the code below and you’re ready to get rollin’.

function makeBitlyUrl($url, $login, $apiKey, $format = 'xml', $version = '2.0.1')
{
	//First, you need to create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apiKey.'&format='.$format;
	
	//Then, get the return data from the API call
	$response = file_get_contents($bitly);
	
	//Parse data depending on your preferred format
	if( strtolower( $format ) == 'json' )
	{
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	}
	else //Default is XML
	{
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

$shortenedUrl = makeBitlyUrl('https://example.com', 'winnie', 'R_123456789abcde', 'json');