Skip to Content

URL shorteners…what for?

People on Twitter are undoubtedly familiar with URL shorteners since in a 140-character message, every character counts. But for the sake of clarity shorteners are popular in a lot of other areas as well. For a good read on URL shorteners and the various ones around check this link from SearchEngineLand.com. Currently there are a lot of services around. Some are the same but some have some nice differences between them. Some redirect you instantly to the page requested (which is convenient in most of the cases) and some have a step in between so the user gets a screen in which he sees where he’s being redirected to. This last step prevents a user from going to malicious sites. Some shorteners offer statistics, so you can see how many times your link is clicked and from where. Some offer the possibility to group your links to an application. Some require authentication, some don’t.

So what did you make?

In ABAP it may be handy to have URLs shortened. For instance by using Twibap (see Code Exchange, project Twibap by Uwe Fetzer). Like I said if you want to post URLs to Twitter you’ll always want to do this with a shortened URL. I created class ZCL_URL_SHORTENER_SERVICE which contains a static public method SHORTEN_URL with the following parameters:

I_VARS Importing Type STRING
I_SHORTENER Importing Type ZUS_SHORTENER
I_ENDPOINT Importing Type STRING
I_SEPARATOR Importing Type STRING
R_URL Returning Type STRING

I_VARS is a string in which you should pass all the parameters needed to call the requested URL shortener separated by I_SEPARATOR (optional, default ;). In I_SHORTENER you can pass the ID of the URL shortener as defined in table ZURLSHORTENER; see below). In I_ENDPOINT you can define a custom URL shortener endpoint. Make sure that all variable parts are replaced by ‘^&*’. Make sure that the amount of variables passed in I_VARS equals the amount of variable parts in the endpoint (either the endpoint in the database or the passed endpoint). R_URL is of course the result and if everything is done correctly it should contain a shortened URL.

You can save all the endpoints in the following table:

ZURLSHORTENER

MANDT MANDT
SHORTENER ZUS_SHORTENER
ENDPOINT ZUS_ENDPOINT
DEFAULT_SERVICE ZUS_DEFAULT

SHORTENER is the ID of the shortener. Endpoint is the URL which should be called to execute the shortener. This can be found on the page of the desired shortener (search for API). Within the endpoint there are variable parts (at least always the URL to be shortened and optionally other parameters like username, API key, application ID, etc.). Make sure you replace the parts that you like to be variable with ‘^&*’. I say ‘that you like’ because I can imagine in some cases you would like to default some parameters. In DEFAULT_SERVICE you define which endpoint should be taken as default (when no data is passed in I_SHORTENER or I_ENDPOINT). It’s a client-dependent table since you might want to store other shorteners in various environments.

I have succesfully tested the following shorteners:

BITLY http://api.bit.ly/v3/shorten?login=^&*&apiKey=^&*&longUrl=^&*&format=txt
CLIGS http://cli.gs/api/v1/cligs/create?url=^&*
CLIGSWACC http://cli.gs/api/v1/cligs/create?url=^&*&title=^&*&key=^&*&appid=^&*
ISGD http://is.gd/api.php?longurl=^&*

To shorten a URL using the default shortener (in my case CLIGS) you can call the method by

DATA l_long_url TYPE string.

DATA l_shortened_url TYPE string.

l_shortened_url = zcl_url_shortener_service=>SHORTEN_URL( l_long_url ).

Cool! Where can we get it?

The SAPLINK package can be downloaded from Code Exchange under project URL SHORTENER. It can be found under Documents and it includes an instruction to get it working. I developed it on a NW7.01 system but it should work on a 7.00 system as well. Not sure about older releases (using SPLIT … INTO TABLE and REPLACE FIRST OCCURRENCE OF…). Currently it is only working with GET methods as that seemed sufficient for me.

Hope you like it!

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Pooja Agrawal

    Hi Roel,

    I have a similar requirement. But i am not able to run the method zcl_url_shortener_service=>SHORTEN_URL. Can you please explain what entry do i need to make in table ZURLSHORTENER.

    I entered

    SHORTENER    ENDPOINT                                                  Default

    CLIGS                http://cli.gs/api/v1/cligs/create?url=^&*        X

    when i call the method    zcl_url_shortener_service=>SHORTEN_URL( http://scn.sap.com/people/roel.vandenberge/blog/2010/11/08/url-shortener-service-in-abap ) I get a dump ‘Exception condition “HTTP_INVALID_STATE” raised.’

    Can you please help.

    Thanks

    Pooja

    (0) 

Leave a Reply