PHP cURL在单个请求中检索响应标头和正文? [英] PHP cURL retrieving response headers AND body in a single request?
本文介绍了PHP cURL在单个请求中检索响应标头和正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用PHP获取cURL请求的标题和正文?我发现这个选项:
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 body plus headers, but then i need to parse it to get the body. Any way to get both in a more usable (and secure) way?
EDIT :对于单个请求,我指的是发出一个HEAD
EDIT: for "single request" i mean avoind issuing a HEAD request prior of GET/POST.
推荐答案
有。详情请参阅在PHP文档中发布: http://www.php.net/manual/en/ function.curl-exec.php#80442
Yes. For details, please take a look at this post in the PHP documentation: http://www.php.net/manual/en/function.curl-exec.php#80442
代码示例:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...
$response = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
这篇关于PHP cURL在单个请求中检索响应标头和正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文