Skip to content

Instantly share code, notes, and snippets.

@m42e
Created January 2, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m42e/9aa1f3e196a996e25381 to your computer and use it in GitHub Desktop.
Save m42e/9aa1f3e196a996e25381 to your computer and use it in GitHub Desktop.
GET2bitly
<?
$login = 'yourlogin';
$apikey = 'yourapikey';
$url = $_GET['URL4BITLY'];
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
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;
}
}
/* usage */
$short = make_bitly_url($url,$login,$apikey,'json');
echo $short;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment