Expand short URL – Simple PHP app for beginners | PHP, HTTP, cURL, Request, Response, Headers

This post is for beginners in PHP. Beginners, who I assume, have said hello to the world. I believe learning is doing. As advised here beginners should start developing something to learn. Let’s do something simple today.

What we are going to do is, to expand any short url. For example, http://goo.gl/YpDP4 should expand into http://blog.sanspace.in. Goal is set. Now, the tools for the job. What are all do we require?

  • A server which can serve PHP
  • A text editor – notepad will do

You should already have a server which serves PHP as you’ve already said hello. Now, the core job. We are going to expand short URLs into long URLs. goo.gl, bit.ly, j.mp, t.co and whatever URL which points to some other long URL will be expanded to their target. Not only short, we will also make redirected URLs to extract into their target URL. Get it? For instance, http://labs.google.com will be extracted into http://www.googlelabs.com/ as the former is redirected to the later.

How to?

How are we going to do that? We must understand a few basic little things to get it done. Heard of HTTP request and response? No? Servers work on request-response method like a Q&A session. You ask something, you get something. Same way you request something, and you get some response. Server responds (answers) you!

Request

Whatever you type on your browser’s address bar is a request. For example, if you type blog.sanspace.in in it’s a request to the server where this Sanspace blog is hosted.

Response

What you get back when you type the URL on the address bar is the response. i.e. After you typed blog.sanspace.in and pressed enter, the blog is shown to you. The entire page shown to you was the content sent as a part of response from the server.

In both the cases, you don’t see the whole thing. Not just blog.sanspace.in is the whole request. Some more information will also be sent along with the URL as request. Likewise, not just the page you get is the response, some other information will also be served with the contest as response.

If you don’t get these things, that won’t be a problem. Let’s go ahead further. You will understand it soon. The below code gets a URL from the user. Requests the server about this URL. After receiving the response, it displays the location property of the response header which is actually the long URL.

//Get response location of a given URL
function eurl($url){
//Get response headers
$response = get_headers($url, 1);
//Get the location property of the response header. If failure, show error
// condition?true:false
return ($response["Location"]?$response["Location"]:$url."<br /><b>No redirection for this URL!!</b>"); 
}

The above code is the simplest for our need. Also, it’s pretty self-explanatory and the comments help too. The live app works here. Try it. You will know what the above code can do.

The source for this app is available here.

Update: Aug 17 2012 00:19:52 Just got this from Command Line Magic ;-) So simple; silly me!!

flattr this!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge