Return to Snippet

Revision: 38557
at January 5, 2011 02:27 by vagrantradio


Initial Code
//Create bit.ly url 
function bitly()
{
	$url = get_permalink();  //get wordpress' permalink
	$login = 'user';	//use your bit.ly login
	$apikey = 'yourbitlyapikey'; //use your bit.ly apikey
	$format = 'json';	//or xml
	$version = '2.0.1';

	//create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;

	//get the url
	$response = file_get_contents($bitly);

	//parse depending on desired format
	if(strtolower($format) == 'json')
	{
		$json = @json_decode($response,true);
		echo $json['results'][$url]['shortUrl'];
	}
	else //xml
	{
		$xml = simplexml_load_string($response);
		echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

// Example of use (must be inside the loop)
<a href="<?php bitly(); ?>">the shortlink</a>

Initial URL


Initial Description


Initial Title
Create bit.ly url

Initial Tags
wordpress

Initial Language
PHP