Created
December 16, 2011 09:21
-
-
Save hzlzh/1485280 to your computer and use it in GitHub Desktop.
Bit.ly URL for WordPress Posts
This file contains hidden or 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
<?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