In the days of link shorteners and Ajax, there can be many links that ultimately point to the same content. I was wondering what the best way is to get the final, best link for a web site in PHP, hopefully with a library. I was unable to find anything on Google or GitHub.

I have seen this example code, but it doesn't handle things like a rel="canonical" meta tags or default ssl ports: <http://w-shadow.com/blog/2008/07/05/how-to-get-redirect-url-in-php/>

Facebook seems to handle this pretty well, you can see how they follow 301's and rel="canonical", etc. To see examples of the way Facebook handles it, use their Open Graph tool:

<https://developers.facebook.com/tools/debug>

and enter these links:

http://dlvr.it/xxb0W  

https://twitter.com/#!/twitter/statuses/136946408275193856

Is there a PHP library out there that already has this pre-built, where it will check for these headers, resolve 301 redirects, parse rel="canonical", detect redirect loops and properly just grab the best resulting URL to use?

As an alternative, I am open to APIs that can be used, but would prefer something that runs on my own server.