PHP cURL可以在单个请求中检索响应标头和正文吗?

本文翻译自: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? 有什么方法可以使用PHP获取cURL请求的标头和正文? 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. 请注意,对于“单个请求”,我的意思是避免在GET / POST之前发出HEAD请求。




curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$parts = explode("\r\n\r\nHTTP/", $response);
$parts = (count($parts) > 1 ? 'HTTP/' : '').array_pop($parts);
list($headers, $body) = explode("\r\n\r\n", $parts, 2);

Works with HTTP/1.1 100 Continue before other headers. HTTP/1.1 100 Continue在其他标头之前HTTP/1.1 100 Continue

If you need work with buggy servers which sends only LF instead of CRLF as line breaks you can use preg_split as follows: 如果需要使用越野车服务器,因为它们仅在换行时发送LF而不发送CRLF,则可以按以下方式使用preg_split

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$parts = preg_split("@\r?\n\r?\nHTTP/@u", $response);
$parts = (count($parts) > 1 ? 'HTTP/' : '').array_pop($parts);
list($headers, $body) = preg_split("@\r?\n\r?\n@u", $parts, 2);


If you specifically want the Content-Type , there's a special cURL option to retrieve it: 如果您特别想要Content-Type ,则有一个特殊的cURL选项来检索它:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);


My way is 我的方式是

$response = curl_exec($ch);
$x = explode("\r\n\r\n", $v, 3);
if ($header=['Response Code']==100){ //use the other "header"

If needed apply a for loop and remove the explode limit. 如果需要,应用for循环并删除爆炸极限。


Just set options : 只需设置选项:



and use curl_getinfo with CURLINFO_HTTP_CODE (or no opt param and you will have an associative array with all the informations you want) 并将curl_getinfo与CURLINFO_HTTP_CODE一起使用(或不使用opt参数,并且您将拥有一个与所需所有信息关联的数组)

More at : 有关更多信息,请访问: http : //


Curl has a built in option for this, called CURLOPT_HEADERFUNCTION. Curl为此有一个内置选项,称为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). Curl会逐行将标头(仅标头!)传递给此回调函数(因此将从标头部分的顶部开始为每个标头行调用该函数)。 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: 这是一个经过测试的工作代码:

function HandleHeaderLine( $curl, $header_line ) {
    echo "<br>YEAH: ".$header_line; // or do whatever
    return strlen($header_line);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "HandleHeaderLine");
$body = curl_exec($ch); 

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. 上面的方法适用于所有内容,不同的协议和代理,并且您不必担心标头大小或设置许多不同的curl选项。

PS: To handle the header lines with an object method, do this: PS:要使用对象方法处理标题行,请执行以下操作:

curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$object, 'methodName'))
  • 0
  • 0
  • 0
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
钱包余额 0