简体   繁体   English

从PHP cURL响应中获取标头

[英]Get Header from PHP cURL response

I am new to PHP. 我是PHP的新手。 I am trying to get the Header from the response after sending the php curl POST request. 我发送php curl POST请求后尝试从响应中获取Header。 The client sends the request to the server and server sends back the response with Header. 客户端将请求发送到服务器,服务器使用Header发回响应。 Here is how I sent my POST request. 这是我发送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 这是我从浏览器获得的服务器的头响应

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中

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

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

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

just use simple code to convert curl response to array. 只需使用简单的代码将curl响应转换为数组。

 $response  = curl_exec($ch);
 $header_data= curl_getinfo($ch);
 print_r($header_data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM