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);
is going to return the body plus headers, but then I need to parse it to get the body. Is there any way to get both in a more usable (and secure) way?
Note that for "single request" I mean avoiding issuing a HEAD request prior of GET/POST.
Curl has a built in option for this, called CURLOPT_HEADERFUNCTION. The value of this option must be the name of a callback function. Curl will pass the header (and the header only!) to this callback function, line-by-line (so the function will be called for each header line, starting from the top of the header section). Your callback function then can do anything with it (and must return the number of bytes of the given line). Here is a tested working code:
The above works with everything, different protocols and proxies too, and you dont need to worry about the header size, or set lots of different curl options.
P.S.: To handle the header lines with an object method, do this:
Return response headers with a reference parameter:
Many of the other solutions offered this thread are not doing this correctly.
\r\n\r\n
is not reliable whenCURLOPT_FOLLOWLOCATION
is on or when the server responds with a 100 code.\n
for new lines.CURLINFO_HEADER_SIZE
is also not always reliable, especially when proxies are used or in some of the same redirection scenarios.The most correct method is using
CURLOPT_HEADERFUNCTION
.Here is a very clean method of performing this using PHP closures. It also converts all headers to lowercase for consistent handling across servers and HTTP versions.
This version will retain duplicated headers
This complies with RFC822 and RFC2616, please do not suggest edits to make use of the
mb_
string functions, it is incorrect!My way is
If needed apply a for loop and remove the explode limit.
If you don't really need to use curl;
Which outputs
See http://php.net/manual/en/reserved.variables.httpresponseheader.php
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/3326494And then to (reliably) get just the body of the request, you would need to pass the value of the
Content-Length
header tosubstr()
as a negative start value.