如何使用curl_setopt在php中生成gzip xml请求/响应
我使用带有curl_setopt的php来获取xml请求和响应。下面是代码i,并使用它。但是我需要在我的请求中应用GZIP,而响应则是GZIP 下面是我现在使用的代码。但是我的XML响应持有者说我没有发送GZIP请求 请更新我下面的代码如何使用curl_setopt在php中生成gzip xml请求/响应,php,xml,gzip,Php,Xml,Gzip,我使用带有curl_setopt的php来获取xml请求和响应。下面是代码i,并使用它。但是我需要在我的请求中应用GZIP,而响应则是GZIP 下面是我现在使用的代码。但是我的XML响应持有者说我没有发送GZIP请求 请更新我下面的代码 $xml_request = '<customer> <product>computer</product> <request> <productid&
$xml_request = '<customer>
<product>computer</product>
<request>
<productid>1001</productid>
</request>
</customer>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING,'Accept-Encoding: gzip,deflate');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
$xml_response = curl_exec($ch);
curl_close($ch);
$xml\u请求=”
计算机
1001
';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$request_URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_ENCODING,'Accept-ENCODING:gzip,deflate');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml_request);
$xml\u response=curl\u exec($ch);
卷曲关闭($ch);
如果您添加
curl_setopt($ch, CURLOPT_ENCODING, "");
它将使用自动支持的任何编码,包括gzip
要检查它是否正常工作,请在curl\u exec调用后添加以下代码:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($xml_response, 0, $header_size);
$body = substr($xml_response, $header_size);
调用curl_exec之前,请执行以下代码:
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
然后echo/var_转储$header以检查是否使用gzip发送了响应
因此,调试代码应如下所示:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING,'');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$xml_response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($xml_response, 0, $header_size);
$body = substr($xml_response, $header_size);
curl_close($ch);
var_dump($header);
当我应用它时,我得到的是空白响应您是否在代码上方定义了$request\u url和xml\u request?删除CULLOPT_编码是否返回响应?可能我在某个地方输入了一个错误:)我的服务器在添加
curl\u setopt($ch,CURLOPT\u ENCODING,'')之后才开始对我的请求使用GZIP代码>。工作起来很有魅力!