Skip to content
Advertisement

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

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

JavaScript

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.

Advertisement

Answer

One solution to this was posted in the PHP documentation comments: http://www.php.net/manual/en/function.curl-exec.php#80442

Code example:

JavaScript

Warning: As noted in the comments below, this may not be reliable when used with proxy servers or when handling certain types of redirects. @Geoffrey’s answer may handle these more reliably.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement