Recently for a project which I was working on, I wanted a way to expand urls shorted by url shorteners.
After a couple of web searches I found an API site called LongURL. This service was really slow and I had to look for alternatives.
I thought of writing my own using curl and then parse the headers. I was searching for the curl functions in PHP Manual and I found a simple but not so famous function called get_headers().
This function is all we need to expand the urls. The following function expand_url() takes a short url and will return the longer version of the url.
Neat function; I didn’t expect it’d be this easy to do.
However, sometimes, an url shortener links to another url shortener; for example: twitter automatically shortens every url tweeted with their t.co shortener. Many people still use other shorteners like bit.ly, then enter it on twitter, which gives you an url that redirects twice.
For this reason, it seems like a good idea to make the function recursive: make it call itself until no location property is found any more. You could add another optional parameter to the function, a count variable passed by reference, which gets to hold the number of redirections detected, if your app would require such a feauture.
That said, it’s a handy function; I’ll bookmark this post for later reference. 🙂
Dude, this is awesome. I was trying to do this using CURL but my host didn’t allow you to follow locations. This is so much easier – thanks for putting it together.
Neat function; I didn’t expect it’d be this easy to do.
However, sometimes, an url shortener links to another url shortener; for example: twitter automatically shortens every url tweeted with their t.co shortener. Many people still use other shorteners like bit.ly, then enter it on twitter, which gives you an url that redirects twice.
For this reason, it seems like a good idea to make the function recursive: make it call itself until no location property is found any more. You could add another optional parameter to the function, a count variable passed by reference, which gets to hold the number of redirections detected, if your app would require such a feauture.
That said, it’s a handy function; I’ll bookmark this post for later reference. 🙂
Right now in my app I don’t need to follow the link. But the use case you have given is valid.
Let me update the post with the recursive function as well.
Dude, this is awesome. I was trying to do this using CURL but my host didn’t allow you to follow locations. This is so much easier – thanks for putting it together.
Thanks man, works great! @ Mich you might be able to use the users twitter RSS feed instead so the redirect is only 1 time : http://twitter.com/statuses/user_timeline/twitterusername.rss
Hi,
Struggling to get this to work with t.co links – has twitter changed something or am I doing something wrong. Testing from a localhost server.
As far as I know, it should just work.
What is the error that you are getting?