PHP - 获取原始服务器响应?
我需要获取带有标头的原始服务器响应。这也意味着 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fsockopen 不会自动解压缩,但如果您正在滚动自己的 HTTP 客户端,则必须告诉服务器您已准备好接受 gzip 压缩的响应,否则服务器将向您发送未压缩的响应。
您可以通过包含 Accept-Encoding 标头来完成此操作在您的要求中,例如
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.