Using googl via PHP to shorten URLs

Google provides an array of free services. One of them being goo.gl, Google’s free URL Shortener. This service is similar to bitly and allows you to save characters in your Tweets and text messages.

If all you need to do is manually shorten URLs, then visit goo.gl and type in the URL which has to be shortened and voilà you’re done.

But if you want to use goo.gl within a PHP application, you will need to make an API call to goo.gl.

Here is a detailed tutorial on how to use this service in a PHP application. There are two main steps involved. First – setting up a Google API key through the Developer Console, and second – making API calls using PHP to shorten URLs.

Generating an API key

  1.  Login to your Google account.
  2. Head to Google Developer Console.
  3. If you already have a Google project, select it. Otherwise use the default project, or create one by selecting “Create A Project” in the header drop down.
  4. Go to ‘Overview’ and select URL Shortening API and enable it by clicking on the blue box that says ‘Enable’ (Refer to image).
  5. After URL Shortening API has been enabled go to ‘Credentials’ in the left menu and select the ‘Credentials’ tab. Then click ‘Create Credentials’ and choose ‘API Key’.
  6.  As we will be making API calls through PHP select ‘Server Key’ and click create.
    generetating google API Key
  7. Type in the name of your key (or use the default one provided) save the key generated. (You can come back to copy it later).

Using PHP to shorten URLs

Now that we have granted ourselves the permission to use goo.gl, we can use it in our PHP application.

I’ve written a simple PHP class that will allow you to do this. You can download this class and use it in your project however you wish. A detailed explanation of how the class works is provided after the the usage.

The PHP CLASS

Using the PHP class

The usage is very simple. And I mean very simple.

  1.  Download the file and save it in you PHP application.
  2. Open the file and inset your API key (the one generated above) in the constructor function.
  3. Include the file where you wish to use it.
  4.  Create an object and call the shorten method.
  5. And you’re done! $shortUrl contains the short URL (duh!)

What the code does

A lot of people use cURL to make API calls. This is fine, but PHP being PHP offers a simple mechanism to make API calls natively.  But before we can make an API call, we need to build  the URL to which we make the request. For this we have the buildUrl() method on the Url class.

This method appends the API key (and custom parameters, if any) to the base URL https://www.googleapis.com/urlshortener/v1/url?. This the URL to which we make our requests (as required by Google).

Once we have the URL, all we have to do is send a POST request to it.  To do this we make use of PHP’s stream_context_create function to build a stream context. We use a stream as we will  access the Google shortener URL using PHP’s file_get_contents function.

The file_get_contents function simply makes the API call and returns the result.

Now all we have to do is extract the shortened URL from the result. The  result is a JSON string which looks like this (if the API call was a success)

We are interested in ‘id’ which is the shortened URL returned by Google. So we use PHP’s json_decode function to convert the JSON string to a JSON object and return the id parameter.

Adarsh

Howdy! You're reading my blog. To know more about me click here

2 thoughts on “Using googl via PHP to shorten URLs

  1. I’ve used bit.ly to shorten links along with jquery so the whole thing is done almost on the client side itself!

    1. Yes, you can use Jquery to make your request too, but this means you will have to expose your API publicly. There may also be problems if the client blocks requests to third party web services. Sending a request from your server ensures that only you and the web service know are aware of the API key.

Leave a Reply

Your email address will not be published. Required fields are marked *