Skip to main content

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');

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.