Created
May 8, 2019 15:21
-
-
Save vielhuber/9d967d7218770c8920ae2319e72c1111 to your computer and use it in GitHub Desktop.
php curl get redirect target #php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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