如何在PHP中获取实际的URL?

I have the url http://www.wolframalpha.com/entities/zip_codes/AL_36574/9t/ej/3z/

when you go to this url it go to http://www.wolframalpha.com/input/?i=AL+36574 here.

Now i want this url from the previous one. therefore i created the code like this

<?php
$url="http://www.wolframalpha.com/entities/zip_codes/AL_36574/9t/ej/3z/";
    $headers = get_headers($url, 1);



// will echo http://www.wolframalpha.com/input/?i=AL+36574
echo $headers[0];
?>

but it does not works Plz help me

Curl would be the best way to find out the redirect. I had this code for my own purposes a while ago, it is not my own. You will have to work out the regex yourself.

<?php
$url="http://www.wolframalpha.com/entities/zip_codes/AL_36574/9t/ej/3z/";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('^0;url=\'(.*?)[$\']+^', $response, $matches);
curl_close($ch);
echo !empty($matches[1]) ? "http://www.wolframalpha.com".trim($matches[1][0]) : 'No redirect found';
?>

You can see it working here http://viper-7.com/TPqpJ8

You have to parse Location from headers and not just output 1st header (HTTP Response Code).

$headers = get_headers($url, 1);
echo $headers['Location'];

Notice how your page returns a 404 response and then redirects using a meta refresh. Reading headers via PHP for this case won't work. PHP can not ordinarily read JavaScript or html redirects that occur after the page has been generated already, Unless you want to try parsing DOM

<meta http-equiv="refresh" content="0;url='/input/?i=AL+36574' " />

Did you try $_SERVER['HTTP_REFERER'] ?

something like this :

if(isset($_SERVER['HTTP_REFERER']))
{
    $a=$_SERVER['HTTP_REFERER'];
    echo "Your previous page URL is ".$a;
}