How to send gziped xml content using curl and php

2019-05-11 15:15发布

My code like that as below, but I cannot get the response successfully. Is anything wrong with my code?

Code:

$headers = array('Content-Type: text/xml;charset=UTF-8','Content-Encoding: gzip',);
$gziped_xml_content = gzencode($xml_content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $the_web_service_url);
curl_setopt($ch, CURLOPT_TIMEOUT,120);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_POSTFIELDS, $gziped_xml_content);
$res = curl_exec($ch);
curl_close($ch);

标签: php xml curl gzip
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-11 16:02

The code is correct. I mean the curl is ok. The error is elsewhere. Your code returns following verbose output which implies the code is correct.

Accept-Encoding: gzip
Content-Type: text/xml;charset=UTF-8
Content-Encoding: gzip
Content-Length: XXX <- some digits

Scenario#1: It can be the server can not handle gzip data. So its throwing you error.

Scenario#2: May be the XML you are sending that has incorrect format, and server failed to parse it and throw you error.

Scenario#3: May be the data you are sending its too large(content length > 1024) for a normal curl post. That case you have to use multipart/form-data form posting.

But before anything, run the curl code with VERBOSE mode enable and it will help you to debug this yourself.

curl_setopt($ch, CURLOPT_VERBOSE, 1);

Finally, I cannot get the response successfully is not a good point to make in your question. Rather using the error you are getting along with more problematic info will help the others who want to help you!

查看更多
登录 后发表回答