【问题标题】:How to send gziped xml content using curl and php如何使用 curl 和 php 发送压缩的 xml 内容
【发布时间】:2014-04-28 12:57:29
【问题描述】:

我的代码如下所示,但我无法成功获得响应。我的代码有什么问题吗?

代码:

$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】:

    代码是正确的。我的意思是卷曲没问题。错误在别处。您的代码在 verbose 输出之后返回,这意味着代码是正确的。

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

    场景#1:可能是服务器无法处理 gzip 数据。所以它会抛出错误。

    场景#2:可能是您发送的 XML 格式不正确,服务器无法解析它并抛出错误。

    场景#3:可能是您发送的数据对于正常的 curl 帖子而言太大(内容长度 > 1024)。这种情况下你必须使用multipart/form-data 表单发布。

    但在此之前,请在启用 VERBOSE 模式的情况下运行 curl 代码,它将帮助您自己调试。

    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    

    最后,I cannot get the response successfully 不适合在您的问题中提出。而是使用您遇到的错误以及更多有问题的信息将帮助其他想要帮助您的人!

    【讨论】:

    • 非常感谢您的回答!我认为Scenario1的可能性更高。我会尝试获取更多信息。再次感谢您!
    猜你喜欢
    • 2013-07-18
    • 2011-08-26
    • 1970-01-01
    • 2012-08-04
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多