在重定向时仅获取最新的HTTP标头

时间:2017-07-05 16:45:15

标签: php php-curl php-7.1

Curl很好地遵循重定向:

$fp = fopen($header, 'wb');
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_WRITEHEADER, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);
curl_close($ch);
fclose($fp);

...但是标题集合包含来自所有中间请求的标题:

HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Jul 2017 16:39:31 GMT
Server: Apache/2.4.25 (Win64) OpenSSL/1.0.2k PHP/7.1.4
X-Powered-By: PHP/7.1.4
Location: http://example.net/
Content-Length: 14
Content-Type: text/html; charset=UTF-8

HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Jul 2017 16:39:31 GMT
Server: Apache/2.4.25 (Win64) OpenSSL/1.0.2k PHP/7.1.4
X-Powered-By: PHP/7.1.4
Location: http://example.org/
Content-Length: 14
Content-Type: text/html; charset=UTF-8

HTTP/1.1 200 OK
Date: Wed, 05 Jul 2017 16:39:31 GMT
Server: Apache/2.4.25 (Win64) OpenSSL/1.0.2k PHP/7.1.4
X-Powered-By: PHP/7.1.4
Content-Length: 5
Content-Type: text/html; charset=UTF-8

由于我经常只对我最终取得的相关内容感兴趣,因为我需要解析整个标头集。

是否有内置的设置/机制来丢弃重定向上的前一个头文件或文本解析是唯一的方法?

1 个答案:

答案 0 :(得分:2)

使用curl_getinfo功能,您可以在重定向后获取实际网址:

CURLINFO_EFFECTIVE_URL

用法示例:

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($last_url === '...') {
    ...
}