PHP cURL:读取特定的响应头 [英] PHP cURL: Read a specific response header
本文介绍了PHP cURL:读取特定的响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在PHP中使用cURL来POST到创建资源的端点。它返回201响应,其Location标题给出了创建的资源的URL。我还在响应正文中获得了一些信息。
I am using cURL in PHP to POST to an endpoint that is creating a resource. It returns a 201 response with a Location header giving the URL of the resource created. I also get some information in the body of the response.
获取响应的纯文本正文的最佳方法是什么,并获取位置的值头? curl_getinfo没有返回那个标题,当我这样做时:
What's the best way to get the plain-text body of the response, and also get the value of the location header? curl_getinfo doesn't return that header, and when I do this:
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) {
var_dump($header);
});
我只看到一个标题被转出 - HTTP / 1.1 201 Created响应代码。
I only see one header dumped out--the "HTTP/1.1 201 Created" response code.
推荐答案
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER,true);
$result = curl_exec($ch);
curl_close($ch);
list($headers, $content) = explode("\r\n\r\n",$result,2);
// Print header
foreach (explode("\r\n",$headers) as $hdr)
printf('<p>Header: %s</p>', $hdr);
// Print Content
echo $content;
这篇关于PHP cURL:读取特定的响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文