PHP Snippet to Expand Shortened URLs

This snippet of PHP code takes any shortened URL, like those from Bitly, Google or Twitter, and expands it into the full and final destination URL.

function expandShortUrl($url)
{
$headers = get_headers($url, 1);

$loc = $headers['Location'];
if(is_array($loc))
{
// get the highest numeric index
$key = max(array_keys( $loc));
return $loc[$key];
}
else
{
return $loc;
}
}

Hat Tip: Deluxe Blog Tips and Karl Groves – Easily Expand Short URL Using PHP