Can PHP cURL retrieve response headers AND body in a single request?

后端 未结 13 1926
粉色の甜心
粉色の甜心 2020-11-22 03:28

Is there any way to get both headers and body for a cURL request using PHP? I found that this option:

curl_setopt($ch, CURLOPT_HEADER, true);
13条回答
  •  独厮守ぢ
    2020-11-22 03:36

    The problem with many answers here is that "\r\n\r\n" can legitimately appear in the body of the html, so you can't be sure that you're splitting headers correctly.

    It seems that the only way to store headers separately with one call to curl_exec is to use a callback as is suggested above in https://stackoverflow.com/a/25118032/3326494

    And then to (reliably) get just the body of the request, you would need to pass the value of the Content-Length header to substr() as a negative start value.

提交回复
热议问题