Posted By

carbonr on 06/18/11


Tagged

url google shorten googl


Versions (?)

goo.gl URL shorten using Un-traditional API - v1


 / Published in: PHP
 

  1. // pass the url you want to shorten in $url when calling the fucntion
  2. // second arugument return the qr code url of the shortened goo.gl url
  3. // getgool("http://www.thisisthelongurltoshorten.com", TRUE);
  4.  
  5. function getgoogl($url, $qr=NULL){
  6. if(function_exists('curl_init')){
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9. curl_setopt($ch, CURLOPT_URL, 'http://goo.gl/api/shorten');
  10. curl_setopt($ch, CURLOPT_POST, TRUE);
  11. curl_setopt($ch, CURLOPT_POSTFIELDS, 'security_token=null&url='.urlencode($url));
  12.  
  13. $results = curl_exec($ch);
  14. $headerInfo = curl_getinfo($ch);
  15. curl_close($ch);
  16.  
  17. if ($headerInfo['http_code'] === 201){ // HTTP Code 201 = Created
  18. $results = json_decode($results);
  19. if(isset($results->short_url)){
  20. $qr = !is_null($qr)?'.qr':'';
  21. return $results->short_url.$qr;
  22. }
  23. return FALSE;
  24. }
  25. return FALSE;
  26.  
  27. }
  28. trigger_error("cURL required to shorten URLs.", E_USER_WARNING); // Show the user a neat error.
  29. return FALSE;
  30. }

Report this snippet  

You need to login to post a comment.