Resolving URLs with PHP

Given a URL, follow redirects and return the final destination:
<?php
function resolve_url($url){
  $ch = curl_init($url);
  
  curl_setopt_array($ch, array(
    CURLOPT_CONNECTTIMEOUT => 10, // 10 second timeout
    CURLOPT_FOLLOWLOCATION => TRUE, // follow redirects
    CURLOPT_NOBODY => TRUE, // uses HEAD
  ));
  curl_exec($ch);
  return curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200 ? curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) : $url;
}