[英]How to make gzip xml request/response in php by curl_setopt
I am using php with curl_setopt to get my xml request and response. 我正在使用带有curl_setopt的php来获取我的xml请求和响应。 Below code i and using for that.
下面的代码我和用于。 But i need to apply GZIP in my request and response comes in GZIP.
但是我需要在请求中应用GZIP,并且响应来自GZIP。
Below is the code i using now. 下面是我现在使用的代码。 But my XML response holders saying i am not sending the GZIP request.
但是我的XML响应持有人说我没有发送GZIP请求。
plz update my below code's 请更新我下面的代码
$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);
If you add 如果添加
curl_setopt($ch, CURLOPT_ENCODING, "");
it will use whatever encoding is supported automatically, including gzip 它将使用自动支持的任何编码,包括gzip
To check if it works correctly, add this code after your curl_exec call: 要检查它是否正常工作,请在curl_exec调用之后添加以下代码:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($xml_response, 0, $header_size);
$body = substr($xml_response, $header_size);
this code before calling curl_exec: 此代码在调用curl_exec之前:
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
then echo/var_dump the $header to check if response been sent using gzip 然后echo / var_dump $ header以检查是否使用gzip发送了响应
so the code for debugging should look like this: 因此,调试代码应如下所示:
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.