Find the True URL of Something, Following Redirects

Have you ever wanted to know the definitive URL to something on the Internet, following any 301 redirects?  I am working on a podcast-related site right now, and recently had to keep track of a multitide of podcast URL's, even when they change.  The following PHP code saved the day:

function get_final_url($url) {
 
  $ch = curl_init($url);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_exec($ch);
  $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  curl_close($ch);  
 
  return $url;
}

To use, let's say you have a url of "http://example123.com/index.html"  but, unbeknownst to you, that URL actually has a 301 redirect to "http://example123.com/welcome.html". 

You can use the function this way:

$url = get_final_url("http://example123.com/index.html");

And $url will now contain "http://example123.com/welcome.html".

Enjoy!