PHP, curl, and raw headers

后端 未结 4 1677
时光取名叫无心
时光取名叫无心 2020-12-13 13:05

When using the PHP curl functions, is there anyway to see the exact raw headers that curl is sending to the server?

相关标签:
4条回答
  • 2020-12-13 13:30

    You can use curl_getinfo:

    Before the call

    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    

    After

    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    
    0 讨论(0)
  • 2020-12-13 13:31

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

    curl_setopt($c, CURLINFO_HEADER_OUT, true);

    0 讨论(0)
  • 2020-12-13 13:38

    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.

    0 讨论(0)
  • 2020-12-13 13:45
    <?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/

    0 讨论(0)
提交回复
热议问题