Cakephp and Googl Url Shortener

This can come in handy if you send out tweats, sms or other texts which should stay as short as possible and could contain urls.
But it can also be used for html emails to track visitor clicks (using the googl analytics overview for those urls).
There are many use cases where such a shortening service might be useful. The googl one is fast and reliable – and for now still without costs.

The source file can be found in my Tools plugin: GooglLib.php

Setup

If you have an API key (which is recommended for the use with analytics and high traffic pages which generate a lot of short urls) define it in your configs:

Configure::write('Googl.key', 'YOURKEY');

Usage

App::uses('GooglLib', 'Tools.Lib');
$this->Googl = new GooglLib();

$longUrl = 'http://www.spiegel.de'; 
$result = $this->Googl->getShort($longUrl);
$shortUrl = $result['id'];

Since this is a lib you can use it anywhere in your application: In the view (inside a helper maybe) or in the model prior to saving or as a behavior for on demand replacing etc.

If you need to get the original url from a short url:

$result = $this->Googl->getLong($shortUrl);
$longUrl = $result['longUrl'];

Other methods

You can use the statisticsUrl method to get an url to the analytics backend for this specific short url.

The getHistory would need oAuth/oacurl to work and is not yet implemented. It would return the analytics data from the above link as an array.

0.00 avg. rating (0% score) - 0 votes

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.