Skip to content

Instantly share code, notes, and snippets.

@codescribblr
Created August 24, 2014 02:10
Show Gist options
  • Select an option

  • Save codescribblr/d6ff2f73e2e5161e8869 to your computer and use it in GitHub Desktop.

Select an option

Save codescribblr/d6ff2f73e2e5161e8869 to your computer and use it in GitHub Desktop.
Using bit.ly v3 api to shorten a url with php.
/* make a URL small */
function make_bitly_url($url,$access_token) {
//create the URL
$bitly = 'http://api-ssl.bitly.com/v3/shorten?access_token='.$access_token.'&longUrl='.urlencode($url);
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
/* usage */
$short = make_bitly_url('http://codescribblr.com','b39e7e013a89e3b836372728e3370b80dd6c6bac');
echo 'The short URL is: '.$short;
// returns: http://bit.ly/11Owun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment