Skip to content

Instantly share code, notes, and snippets.

@hzlzh
Created December 16, 2011 09:21
Show Gist options
  • Save hzlzh/1485280 to your computer and use it in GitHub Desktop.
Save hzlzh/1485280 to your computer and use it in GitHub Desktop.
Bit.ly URL for WordPress Posts
<?php
//create bit.ly url
function bitly()
{
//login information
$url = get_permalink(); //generates wordpress' permalink
$login = '****'; //your bit.ly login
$apikey = 'R_***'; //bit.ly apikey
$format = 'json'; //choose between json or xml
$version = '2.0.1';
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&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);
echo $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment