Consuming URL Shortening Services - Cligs

No.of Views1584
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  02 Sep 2010 11:09:31
Tag : Web Service , Miscellaneous
This is another article that talks about URL shortening services. Today we are going to talk about Cligs, one of the popular shortening services on the web. Be prepared!
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 
This article is also available in my blog, Just Like a Magic.
Read more about URL shortening services here.

Contents

Contents of this article:

  • Contents
  • Overview
  • Introduction
  • Description
  • API
    • Shortening URLs
    • Expanding URLs
  • Where to go next

Overview

This is another article that talks about URL shortening services. Today we are going to talk about Cligs, one of the popular shortening services on the web.

Be prepared!

Introduction

Today we are talking about another popular shortening service; it’s Cligs, one of the most popular shortening services that provide lots of premium features for FREE.

Enough talking, let’s begin the discussion.

In December 2009, Cligs acquired by Mister Wong (a very nice bookmarking service.)

Description

How Cligs can help you? Cligs gives you plenty of features, including the following:

  • Shortening URLs (registered and non-registered users): You get a short URL that’s no more than 20 characters (Tweetburner is 22 and is.gd is only 18) including the domain http://cli.gs.
  • URL Management (registered users only): It allows you to manage your short URLs, to edit them, and to remove them if you like.
  • Real-time Analytics (registered users only): How many clicked your link, and when.
  • URL Previewing (registered and non-registered users): Preview the URL before opening it. Protects you from spam and unwanted sites.

API

Cligs provides you a very nice API with many advantages. The first advantage that we want to talk about is its simplicity. The API is very simple; it has just two functions, one for shortening URLs, and the other for expanding short URLs (to expand a URL means to get the long URL from the short one.)

Another advantage of this API is that it allows you to shorten the URLs whether you are a registered user or not. Of course a registered user need to get an API key in order to link the API calls to his accounts so he can manage the links generated by the API and to watch the analytics.

Shortening URLs

The first function is used for shortening URLs and it’s called, create. This function has the following address:

http://cli.gs/api/v1/cligs/create?url={1}&title={2}&key={3}&appid={4}

The API is still in version 1, that’s why you see ‘v1’ in the address. This function takes four parameters, only the first one is required, other parameters are used for authentication:

  1. url: Required. The URL to be shortened.
  2. title: Optional. For authenticated calls only. The name that would be displayed on the short URL in your control panel (used for managing your URLs.)
  3. key: Optional. If you are a registered user and you want to link the API calls to your account, you’ll need to enter your API key here.
  4. appid: Optional. If you have used an API key, then you have to provide your application name that used to generate this API call (help users know the source of the link.)

So how can you use this function? If this is an anonymous call (i.e. no authentication details provided,) just call the function providing the long URL in its single required argument.

If you need to link this call to a specific account, then you’ll need an API key, which the user can get by signing in to his Cligs account, choosing ‘My API Keys’, then clicking ‘Create New API Key’ (if he doesn’t have one you.) The last step generates an API key that’s no exactly 32 characters (see figure 1.)

Figure 1 - Creating API Keys, Cligs

After you get the API key, you can push it to the function along with your application name in the appid argument.

What about the title argument? For registered users, they can access their clig list (see figure 2) and see the URLs they shortened and the titles they choose above each of the URLs.

 

Now, let’s code! The following function makes use of the Cligs API to shorten URLs. It accepts three arguments, the long URL, the API key, and the application name. If the API key is null (Nothing in VB.NET,) the call is made anonymously, otherwise, the API key and the application name are used.

// C#

string Shorten(string url, string key, string app)
{
    url = Uri.EscapeUriString(url);
    string reqUri =
        String.Format(@"http://cli.gs/api/v1/cligs/create?url={0}", url);
    if (key != null)
        reqUri += "&key=" + key + "&appid=" + app;

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 5000;

    try
    {
        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
' VB.NET

Function Shorten(ByVal url As String, _
                 ByVal key As String, ByVal app As String) As String

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = _
        String.Format("http://cli.gs/api/v1/cligs/create?url={0}", url)
    If key Is Nothing Then
        reqUri &= "&key=" & key & "&appid=" & app
    End If

    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()
        reader.Close()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try

End Function

Expanding URLs

The other function we have today is the expand function that’s used to get the long URL from the short one (e.g. to expand the short URL http://cli.gs/p1hUnW to be http://JustLikeAMagic.com.) This function is very simple and it has the following address:

http://cli.gs/api/v1/cligs/expand?clig={1}

This function accepts only a single argument, that’s the clig (short URL) to be expanded. The clig can be specified using one of three ways:

  • The clig ID. e.g. p1hUnW.
  • The raw URL. e.g. http://cli.gs/p1hUnW.
  • The encoded URL. e.g. http%3A%2F%2Fcli.gs%2Fp1hUnW.
You can read more about URL encoding here.

Now it’s the time for code! The following function takes a clig and returns its original URL:

// C#

string Expand(string url)
{
    url = Uri.EscapeUriString(url);
    string reqUri = String.Format(@"http://cli.gs/api/v1/cligs/expand?clig={0}", url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 5000;

    try
    {
        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
' VB.NET

Function Expand(ByVal url As String) As String

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = _
        String.Format("http://cli.gs/api/v1/cligs/expand?clig={0}", url)

    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()
        reader.Close()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try

End Function

Where to go next

Some other articles about URL shortening services are available here.

 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
    Another article of our endless series that talks about accessing URL shortening services programmatically.This article is talking about 1click.at shortening service, how you can use it, and how to access it via your C#/VB.NET application.
    Published Date : 27/Dec/2010
    This is another article of our URL shortening services series. This article is talking about X.co shortening service provided by Go Daddy. If you don’t know how to access this service from your .NET application, then it’s the time to. We’ll have a complete discussion of the WCF services offered by X.co. Then, we’ll consider the RESTful interfaces provided.
    Published Date : 23/Sep/2010
    This is a very hot article that you can’t leave without checking it first. This article is talking about the most popular and powerful URL shortening service ever, bit.ly. Today, we are going to talk about bit.ly API, its functions, and how you can access them from your .NET application. Don’t forget to download the sample code at the end of the article. Let’s go!
    Published Date : 02/Sep/2010
    Just another article of the URL shortening services series. Today, we are going to talk about another hot and easy-to-use service, it's Tweetburner. If you haven't used it before, then it's the time to. We're going to discuss how to use Tweetburner first. After that, we'll inspect its API and learn how to use it in your .NET application.
    Published Date : 31/Aug/2010
    Another article of our series that talks about accessing URL shortening services programmatically. This article is talking about is.gd shortening service, how you can use it, and how to access it via your C#/VB.NET application.
    Published Date : 30/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top