Created
January 2, 2015 15:42
-
-
Save m42e/9aa1f3e196a996e25381 to your computer and use it in GitHub Desktop.
GET2bitly
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
<? | |
$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