gpt4 book ai didi

php - 如何使用 curl 和 php 发送 gziped xml 内容

转载 作者:可可西里 更新时间:2023-10-31 23:05:34 28 4
gpt4 key购买 nike

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

代码:

$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);

最佳答案

代码是正确的。我的意思是 curl 没问题。错误在别处。您的代码返回以下 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 在您的问题中不是一个好的观点。与其使用你正在相处的错误,还有更多有问题的信息,这将有助于其他想要帮助你的人!

关于php - 如何使用 curl 和 php 发送 gziped xml 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554008/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com