Last active
September 8, 2015 09:55
-
-
Save johnabela/b61b0f9c7ca442b62d97 to your computer and use it in GitHub Desktop.
Use the bit.ly api to generate short URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
/////////////////////////////////////////////////////////////////////// | |
// bitly_MakeShortLink() | |
// | |
// - Description: | |
// Use the bit.ly api to generate short URL | |
// | |
// - Usage: | |
// $ShortURL = bitly_MakeShortLink($url); | |
// | |
// - Variables: | |
// $url = uri to turn into bitly link | |
// | |
// - Author: | |
// John B. Abela <abela@4cm.com> | |
// | |
// - History: | |
// 9/1/2014 1:00 PM - Created | |
/////////////////////////////////////////////////////////////////////// | |
function bitly_MakeShortLink($url) { | |
// | |
// | |
/////////////////////////////////////////////////////////////////////// | |
// Only process if link is over 20 characters | |
/////////////////////////////////////////////////////////////////////// | |
// | |
if ( strlen($url) > 20) { | |
// | |
// | |
/////////////////////////////////////////////////////////////////////// | |
// Acquire shortened bit.ly link | |
/////////////////////////////////////////////////////////////////////// | |
// | |
$bitly_APIURL = 'https://api-ssl.bitly.com/v3/shorten?access_token=' . (BITLY_ACCESS_TOKEN) . '&domain=bit.ly&format=json&longUrl=' . urlencode($url); | |
$bitly_ReturnedData = file_get_contents($bitly_APIURL); | |
$bitly_Results = json_decode($bitly_ReturnedData, true); | |
$bitly_ShortURL = $bitly_Results['data']['url']; | |
// | |
// | |
/////////////////////////////////////////////////////////////////////// | |
// return data | |
/////////////////////////////////////////////////////////////////////// | |
// | |
return trim($bitly_ShortURL); | |
// | |
} | |
else { | |
return trim($url); | |
} | |
// | |
} | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment