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

后端 未结 13 1996
粉色の甜心
粉色の甜心 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 03:56

    If you don't really need to use curl;

    $body = file_get_contents('http://example.com');
    var_export($http_response_header);
    var_export($body);
    

    Which outputs

    array (
      0 => 'HTTP/1.0 200 OK',
      1 => 'Accept-Ranges: bytes',
      2 => 'Cache-Control: max-age=604800',
      3 => 'Content-Type: text/html',
      4 => 'Date: Tue, 24 Feb 2015 20:37:13 GMT',
      5 => 'Etag: "359670651"',
      6 => 'Expires: Tue, 03 Mar 2015 20:37:13 GMT',
      7 => 'Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT',
      8 => 'Server: ECS (cpm/F9D5)',
      9 => 'X-Cache: HIT',
      10 => 'x-ec-custom-error: 1',
      11 => 'Content-Length: 1270',
      12 => 'Connection: close',
    )'
    
    
        Example Domain...
    

    See http://php.net/manual/en/reserved.variables.httpresponseheader.php

提交回复
热议问题