Posted By

vagrantradio on 01/05/11


Tagged

wordpress


Versions (?)

Create bit.ly url


 / Published in: PHP
 

  1. //Create bit.ly url
  2. function bitly()
  3. {
  4. $url = get_permalink(); //get wordpress' permalink
  5. $login = 'user'; //use your bit.ly login
  6. $apikey = 'yourbitlyapikey'; //use your bit.ly apikey
  7. $format = 'json'; //or xml
  8. $version = '2.0.1';
  9.  
  10. //create the URL
  11. $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
  12.  
  13. //get the url
  14. $response = file_get_contents($bitly);
  15.  
  16. //parse depending on desired format
  17. if(strtolower($format) == 'json')
  18. {
  19. $json = @json_decode($response,true);
  20. echo $json['results'][$url]['shortUrl'];
  21. }
  22. else //xml
  23. {
  24. $xml = simplexml_load_string($response);
  25. echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
  26. }
  27. }
  28.  
  29. // Example of use (must be inside the loop)
  30. <a href="<?php bitly(); ?>">the shortlink</a>

Report this snippet  

You need to login to post a comment.