简体   繁体   中英

PHP, curl, and raw headers

使用PHP curl函数时,无论如何都要查看curl 发送到服务器的确切原始标头?

You can use curl_getinfo :

Before the call

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

After

$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
?>

Only available in php 5.1.3 http://php.net/manual/en/function.curl-getinfo.php


You can verify that they are the same by using your console and hitting

curl http://example.com/ -I

or

curl --trace-ascii /file.txt http://example.com/

AFAIK, the PHP/CURL binding still lacks proper support for CURLOPT_DEBUGFUNCTION which is a callback from libcurl that can provide all those details.

That's the primary reason why I recommend people to work out HTTP scripting things with the curl command line tool and its --trace-ascii option FIRST, then translate that into a PHP function.

be sure to set the CURLINFO_HEADER_OUT option before making the curl_getinfo call

curl_setopt($c, CURLINFO_HEADER_OUT, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM