重定向时仅获取最新的 HTTP headers

Get only latest HTTP headers on redirection

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);

...但是 header collection 包括来自所有中间请求的 headers:

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

因为我经常只对我最终获取的内容感兴趣,所以这很不方便,因为我需要解析整个 header 集。

是否有内置的 setting/mechanism 可以在重定向时丢弃之前的 header 或文本解析是唯一的方法?

使用 curl_getinfo 函数你可以在重定向后得到实际的 URL:

CURLINFO_EFFECTIVE_URL

用法示例:

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