/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Create bit.ly url function bitly() { $url = get_permalink(); //get wordpress' permalink $login = 'user'; //use your bit.ly login $apikey = 'yourbitlyapikey'; //use your bit.ly apikey $format = '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 $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; } } // Example of use (must be inside the loop) <a href="<?php bitly(); ?>">the shortlink</a>