如何从php中的重定向url获取目标URL

时间:2013-11-27 03:04:10

标签: php .htaccess redirect curl

我有这个网址,     http://www.example.com

如果您点击此网址,则会将您重定向到此网址,     http://google.com

我希望通过在php中传递重定向网址来获取目标网址。

我已尝试过以下代码,但没有运气,

$headers = get_headers($url);
echo "Success   :".$headers['Location'];

即使我尝试使用像这样的卷曲,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP    follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$a = curl_exec($ch); // $a will contain all headers

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will   return you the last effective URL
echo $url;

我能够看到像这样的输出,

Array ( [0] => HTTP/1.1 200 OK [1] => Date: Wed, 27 Nov 2013 02:53:02 GMT [2] =>    Content-Type: text/html; charset=utf-8 [3] => Content-Length: 2861 [4] => Connection: close [5] => Server: Perl Dancer 1.3118 [6] => Set-Cookie: dancer.session=219345012282242273673421805859294385; path=/; HttpOnly [7] => X-Powered-By: Perl Dancer 1.3118 [8] => Vary: Accept-Encoding ) http://www.example.com

请帮帮我...... !!!

0 个答案:

没有答案