If you have link1 that redirects to link2, but you need to get link2, this is the code
function get_link2($link1) {
$ch = curl_init($link1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response =...