本文翻译自: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 whateverreturn 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'))

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

  1. php http请求xml数据,使用php从HTTP POST请求中检索XML

    我检查了类似的问题但没有解决我面临的问题. 我正在构建一个Web服务,我想从HTTP POST请求中检索XML数据,操纵数据并返回响应.编写脚本时应考虑以下信息: The communication ...

  2. 使用jQuery在AJAX请求中添加标头

    本文翻译自:Add header in AJAX request with jQuery I would like to add a custom header to an AJAX POST req ...

  3. 在 ASP.NET Core 中使用 HTTP 标头传播

    前言 我们常用 JWT 令牌用于身份验证,前端一般是在请求中包含 HTTP 标头 Authorization 实现. 但是,当服务间需要互相调用时,也需要"按原样"将标头传播到目标 ...

  4. postman post请求服务器没响应,Postman POST请求

    Postman POST请求 POST和GET一样是一种HTTP方法.当其他信息需要发送到请求主体内的服务器时,我们使用此方法.通常,当我们提交POST请求时,我们希望服务器上进行一些更改,例如更新, ...

  5. php获取curl头_php中CURL请求头和响应头获取方法

    本文主要和大家分享php中CURL请求头和响应头获取方法,希望能帮助到大家. 1.从CURL中获取响应头$oCurl = curl_init(); // 设置请求头, 有时候需要,有时候不用,看请求网 ...

  6. php curl form-data,在php curl multipart / form-data请求中发送一个文件和json数据

    我正在尝试在PHP的curl请求中上传文件和json数据 . 请求在命令行中使用curl正常工作 . 这是命令行中的curl请求: curl -v --basic -u'username' -F fi ...

  7. php 访问url获得返回值,如何在curl php请求中获取数组值作为返回值?

    我很难使用curl PHP,因为我是 PHP新手.问题是我没有从curl请求中获得任何返回值.我正在访问的远程文件具有以下代码: test.php的: $test->getCall(); pub ...

  8. shell处理curl返回数据_linux shell中curl 发送post请求json格式问题的处理方法

    今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...

  9. 用php批处理图片,PHP中使用CURL发送get/post请求上传图片批处理功能

    cURL是利用url语法规定传输文件和数据的工具.php中有curl拓展,一般用来实现网络抓取,模拟发送get   post请求,文件上传. 在php中建立curl的基本步骤如下: 1.初始化 2. ...

最新文章

  1. (转)ubuntu 文件系统
  2. 把一个中文日期时间格式字符串转为日期时间
  3. python dataframe函数_python pandas中DataFrame类型数据操作函数的方法
  4. HDU2201 熊猫阿波的故事【概率】
  5. Python —— CPU vs. GPU
  6. Java中String字符串长度
  7. 计算机桌面怎样创建文件,如何在电脑桌面新建一个透明文件夹
  8. SymPy学习之Plotting Module
  9. python实现位置定位_python定位位置
  10. kafka:工具:kafkaTool 使用方法
  11. 微信小程序一个你可能需要的功能
  12. python中numpy模块下的np.clip()的用法
  13. 牛客刷题笔记-数据库选择题(201-300)
  14. 【我的Android进阶之旅】Android使用Quantity Strings来实现全球化的单复数功能
  15. 点牛--当FJ点到一头奶牛时,及时提醒他下一头奶牛的编号就可以了。
  16. 双十一来了——策略模式Strategy
  17. String比较大小
  18. oracle ALTER 用法
  19. 计算机中内存问题,存储芯片容量问题
  20. 墨尔本大学计算机科学要求,墨尔本大学新开设计算机科学硕士(附详细学分要求)...

热门文章

  1. Binder相关面试总结(一):为什么Android要采用Binder作为IPC机制?
  2. Activity A 启动 Activity B 生命周期
  3. 第十三周项目三-形状类族中的纯虚函数
  4. CentOS 7.0系统安装配置图解教程
  5. Android之集成微信登录
  6. RxSwift处理Error事件
  7. Flutter开发之PageView指示器(31)
  8. postman断言测试脚本一
  9. pwm控制舵机转动角度程序_01 舵机旋转控制基础
  10. 利用python把成绩用雷达图表示出来