如何获取响应标头?

2022-01-11 00:00:00 header php

我目前正在 PHP CLI 中连接到远程主机并发送一些标头.然后我的脚本等待响应.我可以将其全部打印出来,但随后它会返回 HTML.我只想要标题.我该怎么做?

I'm currently connecting to a remote host in PHP CLI and sending some headers. My script then waits for the response. I can have it all print out, but then it returns the HTML. I only want the headers. How can I do this?

推荐答案

接收HTTP响应的格式与发送请求基本相同:headers和body用两个CRLF( ).

The format for receiving a HTTP response is basically the same as for sending a request: headers and body are separated with two CRLF ( ).

$parts = explode("

", $http_response);
$headers = array_shift($parts);
$body = implode("

", $parts);

相关文章