RSS
 
14. Jul. 2012

Cakephp and Googl Url Shortener

14 Jul

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.

Cakephp and Googl Url Shortener
0 votes, 0.00 avg. rating (0% score)
 
No Comments

Posted by Mark in CakePHP

 

Tags: , , ,

Leave a Reply

Tip:
If you need to post a piece of code use {code type=php}...{/code}.
Allowed types are "php", "mysql", "html", "js", "css".

Please do not escape your post (leave all ", <, > and & as they are!). If you have encoded characters and need to reverse ("decode") it, you can do that here!