Posted By

lasavior on 04/22/11


Tagged

url google short googl


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

AllKnightAccess
Some0ne


Shorten with goo.gl


 / Published in: PHP
 

URL: http://snipt.org/xglpp

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

  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. curl_setopt_array($ch, array(
  7. CURLOPT_POST => TRUE,
  8. CURLOPT_RETURNTRANSFER => TRUE,
  9. CURLOPT_HTTPHEADER => array('Content-type: application/json'),
  10. CURLOPT_POSTFIELDS => json_encode(array('longUrl' => $userurl))
  11. ));
  12. $chresults = json_decode(curl_exec($ch));
  13. curl_close($ch);
  14.  
  15. return $chresults->id;
  16. }

Report this snippet  

You need to login to post a comment.