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请求。


#1楼

参考:https://stackoom.com/question/cWxm/PHP-cURL可以在单个请求中检索响应标头和正文吗


#2楼

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);

#3楼

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);

#4楼

My way is 我的方式是

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

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


#5楼

Just set options : 只需设置选项:

  • CURLOPT_HEADER, 0 CURLOPT_HEADER,0

  • CURLOPT_RETURNTRANSFER, 1 CURLOPT_RETURNTRANSFER,1

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://php.net/manual/fr/function.curl-getinfo.php 有关更多信息,请访问: http : //php.net/manual/fr/function.curl-getinfo.php


#6楼

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, "http://www.google.com");
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

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值