Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Consuming Tinyit.cc Link Shortening API Service in C#.NET/VB.NET/PHP

0.00/5 (No votes)
4 Apr 2011CC (Attr 3U)4 min read 37.4K   485  
This article explains in detail about how to consume the Tinyit.cc link shortening and tracking service API in your C#/VB/PHP based web applications. Tinyit.cc is one of the best URL shorteners and provides a free API to access their spam free link shortening and tracking service.
Tinyit.cc

Introduction

This article explains how to consume the Tinyit.cc link shortening and tracking API. Different explanations for different languages (C#, VB, PHP) have been given. By the end of this article, you would have implemented the Tinyit.cc link shortening web service (API) in your web application. You would have also learnt the basics of how to call web services in your applications using simple HttpWebRequest and HttpWebResponse methods. These are the two methods which are used to send information (request for data) over the web and get a response (fetch data) via HTTP.

A little about what link shorteners actually are. We have seen a rise in number of link shortening services and they are doing a useful job, they are converting long URLs into small useful tiny URLs which are easy to remember and easy to put in websites, blogs, forums, Email, etc. So what this means is this: http://codingzone.net/code/candcplusplus/program-to-define-and-initialize-an-array-and-display-even-elements-stored-in-it-using-cc, which is our long URL and http://tinyit.cc/array, which is our short tiny URL for that long URL. Simple, yet useful. And Tinyit.cc is a simple yet powerful URL shortener through which you can convert long links to short tiny links, customize and share them instantly with your friends and get comprehensive click statistics for your tiny links, viz. unique hits, total hits and along with country, browser and operating system information of the user who clicked your link. Tinyit.cc also checks each URL before redirecting to target URL against Google safe browsing list of websites, phishtank phishing list, spamming list, SURBL list and Tinyit.cc blocked websites list. So the short URLs you create are safe short URLs. Now, let us get onto the basics of what web services are and how to consume them in our web applications.

Background

A little about web services first. A Web service is a method of communication between two electronic devices over a network. Programmers implement web services for a variety of purposes such as to provide a middleware to access a remote database, to provide a middleware to access an application software over the web, etc. Web services basically have a number of procedures and functions that a remote user can call in order to access the service. So a web service for a basic calculator will have different remote procedures such as Add(number1,number2), Subtract(number1,number2), etc. that a user can call in his application if he wants a calculator type functionality in his application.

Now, how to call a web service. Web services can be called in our web applications using simple HttpWebRequest and HttpWebResponse methods. These are the two methods which are used to send information (request for data) over the web and get a response (fetch data) via HTTP. For actual implementation in different languages, read on.

Using the Code

Tinyit.cc provides an easy to use web API through which a user can create short links and get number of hits for a short link. We will implement the API in 3 languages - C#, VB and PHP.

The web URL to access the Tinyit.cc's link shortening API is:

http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY"

Here, LONG_URL_WITHOUT_HTTP is the long URL that needs to be shortened and without the 'http://' header, USERNAME is your Tinyit.cc username and APIKEY is your Tinyit.cc API key. Get a new API key from here.

Here is the simple C#.NET code to create short URL using Tinyit.cc API.

C#
Uri uri = new Uri("http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&
	user=USERNAME&api=APIKEY"); //The API access URL containing the long URL
        string data = "field-keywords=ASP.NET 2.0";
        if (uri.Scheme == Uri.UriSchemeHttp)
        {
            HttpWebRequest request = (HttpWebRequest)
		HttpWebRequest.Create(uri); //New web request object for our long URL
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = data.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(data);
            writer.Close();
            HttpWebResponse response = (HttpWebResponse)
		request.GetResponse(); //Web response object to get the results
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tinyccurl = reader.ReadToEnd(); //Our tiny.cc shortlink
            response.Close();
            Response.Write(tinyccurl);
        }

So, using simple HttpWebRequest and HttpWebResponse methods, we have implemented the Tinyit.cc link shortening API in our application.

Here is the simple VB.NET code to create short URL using Tinyit.cc API.

VB.NET
Dim data As String = "field-keywords=ASP.NET 2.0"
        Dim Uri As New Uri("http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&_
	user=USERNAME&api=APIKEY") 'The API access URL containing the long URL
        If Uri.Scheme = Uri.UriSchemeHttp Then
            Dim request As HttpWebRequest = _
		HttpWebRequest.Create(Uri) 'New web request object for our long URL
            request.Method = WebRequestMethods.Http.Post
            request.ContentLength = data.Length
            request.ContentType = "application/x-www-form-urlencoded"
            Dim writer As New StreamWriter(request.GetRequestStream)
            writer.Write(data)
            writer.Close()
            Dim oResponse As HttpWebResponse = _
		request.GetResponse()  'Web response object to get the results
            Dim reader As New StreamReader(oResponse.GetResponseStream())
            Dim tinyiturl As String = reader.ReadToEnd()
            oResponse.Close()
            Response.Write(tinyiturl)
        End If

Here is the simple PHP code to create short URL using Tinyit.cc API.

PHP
$url = "http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY";
$resource = fopen( $url, 'r' );
$tinyccurl = '';
while (!feof($resource)) {
  $tinyccurl .= fread($resource, 1);
}
fclose($resource);
echo $tinyccurl; //tiny.cc URL

Fetching Number of Hits for the Short URL

The web URL to fetch the number of hits for the short link is:

http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY&getclicks=1"

Just note the new switch we have appended to the API request URL - 'getclicks=1'. This switch signifies that we are interested in fetching the number of hits for our short link rather than creating a new short link. Pretty simple. So the code remains almost the same except that we need to change the API access URL.

Here is the simple C#.NET code to fetch the number of hits for our short URL:

C#
Uri uri = new Uri("http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&
user=USERNAME&api=APIKEY&getclicks=1"); //The API access URL containing the long URL
        string data = "field-keywords=ASP.NET 2.0";
        if (uri.Scheme == Uri.UriSchemeHttp)
        {
            HttpWebRequest request =
            (HttpWebRequest)HttpWebRequest.Create(uri); 	//New web request object 
							//for our long URL
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = data.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(data);
            writer.Close();
            HttpWebResponse response =
            (HttpWebResponse)request.GetResponse(); 	//Web response object 
						//to get the results
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string hits = reader.ReadToEnd(); //Our hits for the shortlink
            response.Close();
            Response.Write(tinyccurl);
        }

So, using simple HttpWebRequest and HttpWebResponse methods, we have implemented the Tinyit.cc link shortening API in our application.

Here is the simple VB.NET code to fetch the number of hits for our short URL:

VB.NET
Dim data As String = "field-keywords=ASP.NET 2.0"
        Dim Uri As New Uri("http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&_
        user=USERNAME&api=APIKEY&getclicks=1") 'The API access URL containing the long URL
        If Uri.Scheme = Uri.UriSchemeHttp Then
            Dim request As HttpWebRequest = _
		HttpWebRequest.Create(Uri) 'New web request object for our long URL
            request.Method = WebRequestMethods.Http.Post
            request.ContentLength = data.Length
            request.ContentType = "application/x-www-form-urlencoded"
            Dim writer As New StreamWriter(request.GetRequestStream)
            writer.Write(data)
            writer.Close()
            Dim oResponse As HttpWebResponse = _
		request.GetResponse()  'Web response object to get the results
            Dim reader As New StreamReader(oResponse.GetResponseStream())
            Dim hits As String = reader.ReadToEnd() 'Our hits for the shortlink
            oResponse.Close()
            Response.Write(tinyiturl)
        End If

Here is the simple PHP code to fetch the number of hits for our short URL:

PHP
$url = "http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&
	user=USERNAME&api=APIKEY&getclicks=1";
$resource = fopen( $url, 'r' );
$tinyccurl = '';
while (!feof($resource)) {
  $tinyccurl .= fread($resource, 1);
}
fclose($resource);
echo $tinyccurl; //tiny.cc URL

Pretty simple and quick. All the short links you create via API will be available in your Tinyit.cc user control panel. You can access the link statistics from here.

History

  • 4th April, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License