Welcome Guest, Not a member yet? Register   Sign In
Resolvoing Shortened URL
#1

[eluser]jmanpa[/eluser]
What approach might be best, using CI libraries or not, to take a url, say "http://bit.ly/somestuff" and somehow resolve and return the real url underneath. Assume the shortening service could be any, not just bit.ly.

Not knowing beforehand whether the url is shortened or not, the approach should not blow up if the source url was never shortened in the first place and is a valid url.

Thanks,
John
#2

[eluser]WanWizard[/eluser]
Fetch the URL using a HEAD request.

If you receive a status code 200, it's a normal link. If you recieve a 404, the link does not exist. If you get a 301, the Location field in the header contains the real url.
#3

[eluser]jmanpa[/eluser]
Excellent! Thanks.
#4

[eluser]WanWizard[/eluser]
Note: this is under the assumption that they use a 301 redirect (bit.ly does). You'll have to test other ones, and catch other result codes as exception.




Theme © iAndrew 2016 - Forum software by © MyBB