Google Shorten App is very useful for any WordPress site to make short URL. If you want to send to anyone a URL, it ‘s too bad to see the URL is too long. You know, the long URL is only take their time look entire of URL and it have no meaning with them.
I am sorry if I go too far from the main problem, our problem is we are using an plugin/extension/theme what need you provide a Google Shorten API Key or Goo.gl API Key, now I show you how to get it easy.
First of all, let ‘s login to your Google Account, if you don’t have one, please sign up the new one (I think it ‘s more than easy so that I will not tell you how to create Google account here).
Visit http://code.google.com/apis/console/ and browse to the left menu APIs & Auth => APIS
Now search and turn on URL Shortener API
Now browse to menu APIs & Auth => Credentials and click CREATE NEW KEY under Public API Access. In lightbox confirm choose Server key and you will see the box to enter your server IP as below:
After click Create then you will see the API Key you need. It will look like this
Try to call Goo.gl API in PHP
Here is simple function in PHP to convert a long URL to shorten URL
//This is the URL you want to shorten
$apiKey = 'AIzaSyDMgK9gpIIokl7ClMqMXHZQ7wv_xzRwMHs';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
btw, I am very happy to receive reviews from many users about goo.gl api and welcome any comments.