从 PHP cURL 响应中获取标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41978957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Header from PHP cURL response
提问by Tuan Dinh
I am new to PHP. I am trying to get the Header from the response after sending the php curl POST request. The client sends the request to the server and server sends back the response with Header. Here is how I sent my POST request.
我是 PHP 新手。我试图在发送 php curl POST 请求后从响应中获取 Header。客户端向服务器发送请求,服务器返回带有 Header 的响应。这是我发送 POST 请求的方式。
$client = curl_init($url);
curl_setopt($client, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($client, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($client, CURLOPT_HEADER, 1);
$response = curl_exec($client);
var_dump($response);
Here is the Header response from server that I get from the browser
这是我从浏览器获得的来自服务器的 Header 响应
HTTP/1.1 200 OK
Date: Wed, 01 Feb 2017 11:40:59 GMT
Authorization: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2Vycy9CYW9CaW5oMTEwMiIsIm5hbWUiOiJhZG1pbiIsInBhc3N3b3JkIjoiMTIzNCJ9.kIGghbKQtMowjUZ6g62KirdfDUA_HtmW-wjqc3ROXjc Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Server: Jetty(9.3.6.v20151106)
How can I extract the Authorization part from the Header ?. I need to store it in the cookies
如何从标题中提取授权部分?。我需要将它存储在 cookie 中
回答by Thamaraiselvam
It converts all headers into an array
它将所有标题转换为数组
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$headers = [];
$output = rtrim($output);
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);
foreach($data as $part){
//some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
$middle = explode(":",$part,2);
//Supress warning message if $middle[1] does not exist, Thanks to @crayons
if ( !isset($middle[1]) ) { $middle[1] = null; }
$headers[trim($middle[0])] = trim($middle[1]);
}
// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";
回答by myHealthbox Admin
For the first answer note that the code:
对于第一个答案,请注意代码:
$middle=explode(":",$part);
will produce wrong results with string data containing :
, like for example:
使用包含 的字符串数据会产生错误的结果,:
例如:
Sat, 14 Jan 2017 01:10:01 GMT
The correct code to split the fields to build the array would be like:
拆分字段以构建数组的正确代码如下:
$middle=explode(":",$part,2);
回答by karthikeyan ganesan
You just include this coding into your curl request
您只需将此编码包含在 curl 请求中
curl_setopt($curl_exec, CURLOPT_HEADER, true);
curl_setopt($curl_exec, CURLOPT_NOBODY, true);
after your curl execution use $header_data= curl_getinfo($curl_exec);
在 curl 执行使用后 $header_data= curl_getinfo($curl_exec);
Then you get all the headers
然后你得到所有的标题
print_r($header_data);
or use the shell_exec
或使用shell_exec
echo shell_exec("curl -I http://example.com ");
回答by Sourav
just use simple code to convert curl response to array.
只需使用简单的代码将 curl 响应转换为数组。
$response = curl_exec($ch);
$header_data= curl_getinfo($ch);
print_r($header_data);