如何使用PHP中的curl发送Accept Encoding标头
$data=json_encode($data);
$url = 'url to send';
$headers = array(
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
$datas = curl_exec($ch);
curl_close($ch);
如何解压缩响应
答案 0 :(得分:3)
您可以使用CURLOPT_ENCODING
:
curl_setopt($ch, CURLOPT_ENCODING, "");
" Accept-Encoding的内容:"头。这使得能够解码 的回应。支持的编码是" identity"," deflate",和 " gzip的&#34 ;.如果设置了空字符串"",则包含全部的标题 发送支持的编码类型。
http://php.net/manual/en/function.curl-setopt.php
或者,您可以发送标题:
$headers = array(
'Accept: text/plain'
);
在txt
答案 1 :(得分:0)
如果你的意思是如何ungzip响应,我这样做:
<?php
....
$headers = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.3",
"Accept-Language:en-US;q=0.6,en;q=0.4",
"Connection: keep-alive",
);
....
$response = curl_exec($curl);
// check for curl errors
if ( strlen($response) && (curl_errno($curl)==CURLE_OK) && (curl_getinfo($curl, CURLINFO_HTTP_CODE)==200) ) {
// check for gzipped content
if ( (ord($response[0])==0x1f) && (ord($response[1])==0x8b) ) {
// skip header and ungzip the data
$response = gzinflate(substr($response,10));
}
}
// now $response has plain text (html / json / or something else)