PHP - 获取原始服务器响应?

发布于 2024-12-09 20:44:30 字数 1280 浏览 0 评论 0原文

我需要获取带有标头的原始服务器响应。这也意味着 gzip 压缩或压缩的内容仍应被压缩。我不希望对收到的内容进行任何更改。

这可以用 PHP 实现吗?

我尝试使用curl,但这似乎不起作用,我将它们设置为零:

             CURLOPT_HTTP_CONTENT_DECODING => 0, 
             CURLOPT_HTTP_TRANSFER_DECODING => 0,

但没有帮助。

我尝试使用 fsockopen 但似乎也会自动解压缩。

还要别的吗?

编辑:这些都是我的卷曲标题:

$options = array(CURLOPT_URL => 'http://www.example.com/', 
             CURLOPT_CONNECTTIMEOUT => 20,
             CURLOPT_HEADER => 1, 
             CURLOPT_RETURNTRANSFER => 1, 
             CURLOPT_FOLLOWLOCATION => 1, 
             CURLOPT_USERAGENT => $user_agent, 
             //CURLOPT_CUSTOMREQUEST => 'HEAD',
             //CURLOPT_NOBODY => true,
             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                 //CURLOPT_HTTP_CONTENT_DECODING => 0, 
                 //CURLOPT_HTTP_TRANSFER_DECODING => 0,
             CURLOPT_BINARYTRANSFER => 1,
             CURLOPT_HTTPHEADER => array('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
                                         'Accept-Language' => 'en-us', 
                                         'Accept-Encoding' => 'gzip, deflate'));

谢谢。

I need to get the raw server response, with headers. This also means that gzipped or deflated content should still be compressed. I don't want any changes done to what is received.

Is this possible with PHP?

I tried with curl but that doesn't seem to be working, I set these to zero:

             CURLOPT_HTTP_CONTENT_DECODING => 0, 
             CURLOPT_HTTP_TRANSFER_DECODING => 0,

But no help.

I tried with fsockopen but that seems to uncompress automatically as well.

Anything else?

Edit: these are all my curl headers:

$options = array(CURLOPT_URL => 'http://www.example.com/', 
             CURLOPT_CONNECTTIMEOUT => 20,
             CURLOPT_HEADER => 1, 
             CURLOPT_RETURNTRANSFER => 1, 
             CURLOPT_FOLLOWLOCATION => 1, 
             CURLOPT_USERAGENT => $user_agent, 
             //CURLOPT_CUSTOMREQUEST => 'HEAD',
             //CURLOPT_NOBODY => true,
             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                 //CURLOPT_HTTP_CONTENT_DECODING => 0, 
                 //CURLOPT_HTTP_TRANSFER_DECODING => 0,
             CURLOPT_BINARYTRANSFER => 1,
             CURLOPT_HTTPHEADER => array('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
                                         'Accept-Language' => 'en-us', 
                                         'Accept-Encoding' => 'gzip, deflate'));

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-12-16 20:44:30

fsockopen 不会自动解压缩,但如果您正在滚动自己的 HTTP 客户端,则必须告诉服务器您已准备好接受 gzip 压缩的响应,否则服务器将向您发送未压缩的响应。

您可以通过包含 Accept-Encoding 标头来完成此操作在您的要求中,例如

Accept-Encoding: compress, gzip

fsockopen won't automatically decompress, but you if you are rolling your own HTTP client, you must tell the server you're ready to accept a gzipped response, other the server will send you an uncompressed one.

You can do this by including an Accept-Encoding header in your request, e.g.

Accept-Encoding: compress, gzip
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文