Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created May 8, 2019 15:21
Show Gist options
  • Save vielhuber/9d967d7218770c8920ae2319e72c1111 to your computer and use it in GitHub Desktop.
Save vielhuber/9d967d7218770c8920ae2319e72c1111 to your computer and use it in GitHub Desktop.
php curl get redirect target #php
<?php
function get_redirect_final_target($url)
{
$ch = curl_init($url);
// if you need htaccess htpasswd
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_NOBODY, 1);
// follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// set referer on redirect
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_exec($ch);
$target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
if ($target) {
return $target;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment