Generar links en bit.ly con PHP

// July 16th, 2009 // Programacion

Esta función, igual que la de tinyurl, pero para Bit.ly esta escrita en PHP usando el API de bit.ly y sirve para “acortar” Urls, la función es muy practica y útil. El código al salto.

<?
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
	$response = file_get_contents($bitly);
	if(strtolower($format) == 'json')
	{
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	}
	else //xml
	{
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

/* uso */
$acortado = make_bitly_url('http://www.ajpl.com.ar','usuario','apikeydelusuario','json');
echo 'La URL corta es:  '.$acortado; 

// $acortado seria igual a http://bit.ly/11msFx
?>

Leave a Reply