Guest User

Google URL SHortener PHP function

a guest
Aug 9th, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  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. ?>
Add Comment
Please, Sign In to add comment