I want to use PHP to get the URL of the page to which the following address redirects:
The script should return the following URL to which the URL above redirects:
I want to use PHP to get the URL of the page to which the following address redirects:
The script should return the following URL to which the URL above redirects:
One way (of many) to do this is to open the URL with fopen
, then use stream_get_meta_data
to grab the headers. This is a quick snippet I grabbed from something I wrote a while back:
$fh = fopen($uri, 'r');
$details = stream_get_meta_data($fh);
foreach ($details['wrapper_data'] as $line) {
if (preg_match('/^Location: (.*?)$/i', $line, $m)) {
// There was a redirect to $m[1]
}
}
Note you can have multiple redirections, and they can be relative as well as absolute.
You can do this using cURL.
<?php
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
//$header['errno'] = $err;
// $header['errmsg'] = $errmsg;
//$header['content'] = $content;
print($header[0]);
return $header;
}
$thisurl = "http://www.example.com/redirectfrom";
$myUrlInfo = get_web_page( $thisurl );
echo $myUrlInfo["url"];
?>
Code found here: http://forums.devshed.com/php-development-5/curl-get-final-url-after-inital-url-redirects-544144.html
I've found this resource to be the most complete, thought-out approach and explanation. The code isn't the shortest snippit, but you'll end up being able to track multiple redirects with a couple lines like this:
$result = get_all_redirects('http://bit.ly/abc123');
print_r($result);