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

后端 未结 13 2002
粉色の甜心
粉色の甜心 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:52

    Just in case you can't / don't use CURLOPT_HEADERFUNCTION or other solutions;

    $nextCheck = function($body) {
        return ($body && strpos($body, 'HTTP/') === 0);
    };
    
    [$headers, $body] = explode("\r\n\r\n", $result, 2);
    if ($nextCheck($body)) {
        do {
            [$headers, $body] = explode("\r\n\r\n", $body, 2);
        } while ($nextCheck($body));
    }
    

提交回复
热议问题