简体   繁体   English

PHP curl和gzip:浏览器无法处理gzip响应

[英]PHP curl and gzip: browser not handling gzip response

I) My proxy server passes the Accept-Encoding header that it received from the client to my back-end content server as-it-is. I)我的代理服务器将它从客户端收到的Accept-Encoding标头按原样传递给我的后端内容服务器。 Data received from backend server is sent to the requesting browser simply using PHP echo (without processing headers separately). 从后端服务器接收的数据仅使用PHP echo(无需单独处理标头)发送到发出请求的浏览器。 However, the data gets displayed in the browser as raw binary data. 但是,数据将作为原始二进制数据显示在浏览器中。

If I do not pass any Accept-Encoding to my content server, then everything is fine. 如果我没有将任何Accept-Encoding传递给我的内容服务器,那么一切都很好。

Since the browser accepts GZIP data, why does my proxy server need to decode it - why doesn't directly passing the gzip data back to the browser work? 由于浏览器接受GZIP数据,为什么我的代理服务器需要对其进行解码-为什么不将gzip数据直接传递回浏览器呢? Rather than gunzipping the data on proxy server, is there some setting I am missing that will make things work? 而不是将数据压缩在代理服务器上,我缺少一些可以使事情正常进行的设置吗?


II) Based on stillstanding's suggestions, I tried a new way (but now I am more confused as it has its own problems)!. II)根据stillstanding的建议,我尝试了一种新方法(但是现在我更加困惑,因为它有自己的问题)!

I used 我用了

....
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
$result = curl_exec($curl_handle);
curl_close($curl_handle);

and instead of echoing the result, I do: 我没有回显结果,而是:

list($headers,$content)=explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr) {
     header($hdr);
}
echo $content;

Now, the browser recognizes that the data is gzipped, but gives option to save the gzip file instead of displaying the contents. 现在,浏览器可以识别出数据已压缩,但是可以选择保存gzip文件而不显示内容。 As mentioned earlier, I am just passing the Accept-Encoding that browser gives me, then why the issue? 如前所述,我只是传递了浏览器提供给我的Accept-Encoding,那么为什么会这样呢?

thanks 谢谢

JP J.P

PS: (I have seen some other questions on SO related to curl gzip, but they are unable to resolve my doubt). PS:(我在SO上还看到了其他一些与curl gzip有关的问题,但他们无法解决我的疑问)。

The comments, some more google search and trial and error helped me fix the problem. 评论,更多的谷歌搜索和反复试验帮助我解决了这个问题。

My curl was set (without me doing anything) to automatically deflate the response sent by the backend server. 我的curl被设置为(不做任何事情)自动缩小后端服务器发送的响应。 So the headers received by the browser said "gzip", but data received was not. 因此,浏览器收到的标头显示为“ gzip”,但没有收到数据。

Stopped curl from deflating the response by: 通过以下方式阻止了卷曲使响应收缩:

curl_setopt($curl_handle, CURLOPT_ENCODING, "identity");

and now I can just echo the result as it is and works fine. 现在我可以按原样回显结果,并且可以正常工作。

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

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