Google URL shortener with PHP and Google Api

On my previous post I was shown how to shorten URL using Bitly API and PHP. In this post I will show you how you can shorten URL using Google URL shortener api using PHP. Google also provide URL shortener service (goo.gl) for its users. To working with this API you will need to get a Google URL Shorten API key. If you do not have get one from here for free.


Google URL Shorten API


Google URL Class:

/**
* Google URL shorten API
* @version API V1
*/
class GoogleURL
{
 /**
  * API URL
  * @var string
  */
 private $apiURL = 'https://www.googleapis.com/urlshortener/v1/url';

 /**
  * Google URL shorten Constructor
  * @param string $apiKey
  * @return void
  */
 function __construct($apiKey)
 {
  # API URL with API keys
  $this->apiURL = $this->apiURL . '?key=' . $apiKey;
 }

 /**
  * Short Long URL
  * @param string $url as long URL
  * @return string as short URL or void
  */
 public function encode($url)
 {
  $data = $this->cURL($url, true);
  return isset($data->id) ? $data->id : '' ;
 }

 /**
  * Extend Short URL
  * @param string $url as short URL
  * @return string as as long URL or void 
  */
 public function decode($url)
 {
  $data = $this->cURL($url, false);
  return isset($data->longUrl) ? $data->longUrl : '' ;
 }

 /**
  * Send cURL Request
  * @param string $url
  * @param bool $post
  * @return object
  */
 private function cURL($url, $post = true)
 {
  # create cURL
  $ch = curl_init();
  # POST request for URL shorten
  if ($post) {
   curl_setopt( $ch, CURLOPT_URL, $this->apiURL );
   # set header content type for json
   curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json') );
   # set request method post
   curl_setopt( $ch, CURLOPT_POST, true );
   # send json encoded request
   curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $url)) );
  }
  # GET request for URL extend
  else {
   curl_setopt( $ch, CURLOPT_URL, $this->apiURL . '&shortUrl=' . $url );
  }
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  #curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  # execute curl request
  $json = curl_exec($ch);
  # close cURL connection
  curl_close($ch);
  # return response as object
  return (object) json_decode($json);
 }
}

Usages:

$api = new GoogleURL('put_your_api_key_here');
# shorten url
echo $api->encode('http://google.com');
# extend url
echo $api->decode('http://goo.gl/mR2d');

6 comments:

  1. Nice tutorial! Carry on......

    ReplyDelete
  2. Awesome work.Just wanted to drop a comment and say I am new to your blog and really like what I am reading.Thanks for the share

    ReplyDelete
  3. does not generate the short url, I had to change the text / json for application / json

    ReplyDelete
  4. I have this error whith your code and the Google Shorter URL api :
    This API does not support parsing form-encoded input.

    ReplyDelete
    Replies
    1. there are a update, you need change
      curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json') );
      for ->
      curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );

      Delete

Go to top