<?php
$url = 'http://fb.com';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
));
$header = explode("\n", curl_exec($curl));
curl_close($curl);
print_r($header);
Run Code Online (Sandbox Code Playgroud)
结果
HTTP/1.1 301 Moved Permanently
Location: http://www.facebook.com/?_rdr
Vary: Accept-Encoding
Content-Type: text/html
X-FB-Debug: rVg0o+qDt9z/zJu7jTW1gi1WSRC8YIMu3e6XnPagx39zZ4pbV0k2yrNfZmkdTLZyfzg713X+M0Lr2jS2P018xA==
Date: Thu, 25 Feb 2016 08:48:08 GMT
Connection: keep-alive
Content-Length: 0
Run Code Online (Sandbox Code Playgroud)
但我希望Location一次得到所有
I enter > http://fb.com
then 301 redirect: http://www.facebook.com/?_rdr
then 302 redirect: https://www.facebook.com/
Run Code Online (Sandbox Code Playgroud)
我希望一次获得所有这个链接状态 301 302
或任何更好的想法来获取重定向位置网址.谢谢
小智 3
您可以从每个请求中获取所有标头,直到没有Location使用此标头发送标头:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
但是,你必须自己提取信息,因为$headers它只是一个字符串,而不是一个数组.
如果您只需要最后一个位置,那就干嘛curl_getinfo($ch,CURLINFO_EFFECTIVE_URL).