SHARE
TWEET

Google URL SHortener PHP function

a guest Aug 9th, 2011 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     function googl($longUrl)
  3.     {
  4.         $post = json_encode(array('longUrl' => $longUrl));
  5.         $ch = curl_init();
  6.         curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
  7.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  9.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  10.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  11.         curl_setopt($ch, CURLOPT_POST, true);
  12.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  13.         $result = json_decode(curl_exec($ch));
  14.         curl_close($ch);
  15.         if(isset($result->id)) return $result->id;
  16.         return false;
  17.     }
  18.    
  19.     echo googl('http://www.google.com');           
  20. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top