Shorten with goo.gl


/ Published in: PHP
Save to your folder(s)

I needed a simple function to shorten a link with goo.gl and wrote this, thought it may help someone. Note: you still need an API key from goo.gl


Copy this code and paste it in your HTML
  1. function getgoogl($glurl, $apikey) {
  2.  
  3. $userurl = rawurlencode($glurl);
  4.  
  5. $ch = curl_init('https://www.googleapis.com/urlshortener/v1/url?key='.$apikey);
  6. CURLOPT_POST => TRUE,
  7. CURLOPT_RETURNTRANSFER => TRUE,
  8. CURLOPT_HTTPHEADER => array('Content-type: application/json'),
  9. CURLOPT_POSTFIELDS => json_encode(array('longUrl' => $userurl))
  10. ));
  11. $chresults = json_decode(curl_exec($ch));
  12. curl_close($ch);
  13.  
  14. return $chresults->id;
  15. }

URL: http://snipt.org/xglpp

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.