Author: Eslam Mahmoud
Updated on: 2015-01-18
Posted on: 2011-10-13
Package: Goo.gl short URL
It also explains how to achieve the opposite, i.e. take a goo.gl short URL, retrieve the respective long URL, as well obtaining analytics information about the clicks on the short URL links.
In this post I will describe how to use the Google URL Shortener PHP Class.
First, the purpose of the class is to shorten or expand an URL using the goo.gl Google service.
It sends a HTTP request to Google goo.gl URL shortener Web services API to generate a short URL for another given URL.
The class also send a request to expand a given shortened URL with the same Google goo.gl service.
Since version 0.2 it also can get analytics information about the accesses to the short url when retrieve the expanded URL.
Now lets dig into how to use the class. First you need to create an object of the class like this:
$goo = new Googl();
If you have an API key for the goo.gl service, you should specify it as a constructor parameter. You need to request an API key in Google site.
$goo = new Googl( "API Key" );
Get a short URL
To get a short URL you need to call the get_short function and pass the URL as a string parameter like this:
$r = $goo->get_short( "http://eslam.me" );
Expand a short URL
To expand a previously shortened URL call the get_long function passing the short url as first parameter.
Pass true as the second parameter value if you want get the access analytics information for that URL. This will make the class return statistics about the number of times the URL was followed, the browsers and countries of the users that clicked on links with the short URL, as well the referrer pages where the short URL links were clicked.
$r = $goo->get_long( "http://goo.gl/pTgKg", true); print_r($r);
Please help spreading this class URL by sharing this short URL that points to the class page: goo.le .
You need to be a registered user or login to post a comment
1,451,806 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on: