我有一个API,可以在发送POST数据后返回结果。
目前我正在使用fopen。
我被告知,当发送正确的标头时,API将返回gzip压缩结果。
如何使用fopen发送Accept Encoding标头?
目前,相关代码如下:
$params = array('http' => array(
'method' => 'POST',
'content' => $xml,
'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
'ignore_errors' => $ignoreErrors
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
Google中很容易找到的所有示例/等与尝试读取始终是gzip压缩的流的人或想要设置标题以便他们发送压缩数据的人有关。
答案 0 :(得分:3)
我无法使用fopen / stream_get_contents来使用它。
根据其他一条建议,我试过了CURL。
出于某种原因,以下任何一项都无效:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
我也尝试了其他一些建议。
以下工作:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
答案 1 :(得分:1)
尝试添加一个空的Accept-Encoding
标题:
$params = array('http' => array(
'method' => 'POST',
'content' => $xml,
'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n"
.'Accept-Encoding:' . "\r\n"
'ignore_errors' => $ignoreErrors
));
空值与identity
相同,这意味着不应该进行编码。请参阅我已链接的rfc。
但请注意,您无法确定远程服务器是否尊重这一点。
答案 2 :(得分:0)
向标题参数添加“Accept-Encoding:gzip,deflate”。而且你必须在那里连接可选的标题以便全部发送它们,目前你只是替换它们