Php 在重定向时仅获取最新的HTTP头
Curl很好地遵循重定向:Php 在重定向时仅获取最新的HTTP头,php,php-curl,php-7.1,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);
$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
因为我通常只对最终获取的内容感兴趣,这相当不方便,因为我需要解析整个头集
是否有一种内置设置/机制,可以在重定向时丢弃以前的标题,或者只有通过文本解析才能实现?使用该函数,您可以在重定向后获取实际URL:
CURLINFO_EFFECTIVE_URL
用法示例:
$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($last_url === '...') {
...
}
哦,这太好了。有许多选项,如
CURLINFO\u CONTENT\u TYPE
。严格地说,这并不能回答我的问题,但它最终解决了我的问题。非常感谢你。