Magento Soap API V2 响应内容长度不正确 [英] Magento Soap API V2 Response Content Length Incorrect

查看:19
本文介绍了Magento Soap API V2 响应内容长度不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将我们的 magento 安装与第三方库存管理应用程序(构建在 .net 上)连接起来.但是同步不起作用,第三方告诉我soap api 返回空响应.

We are trying to connect our magento installation with a third party stock management app(built on .net). But the sync is not working, the third party tells me that the soap api is returning an empty response.

我一直在竭尽全力,因为每当我用 PHP 制作任何东西时,API 都能正常工作.此外,库存更新工作正常,但检索订单/发票信息却没有(实际上偶尔会发生 1% 的情况)

I have been pulling my hear out, because whenever I make anything in PHP the API works fine. In addition inventory updating works fine, but retrieving order/invoice information does not (well it actually does occasionally 1% of the time)

由于间歇性,我们认为它一定是网络问题,但是经过大量搜索并将 mage::log() 添加到核心 api 文件后,我可以看到连接正在发生,而且响应对象是由 Magento 创建.

With the intermittent nature we felt that it must be a network issue, but after lots of searching and adding mage::log() into the core api files I can see that the connection is happening and further more the response object is being created by Magento.

所以我的推断是因为 SOAP API 有问题(我使用的是版本 2)

So my deduction as that something was not right with the SOAP API (I am using version 2)

我已经安装了 soapUI 并设置了我们的集成,它正确地接收了来自 WSDL 文件的方法,但是当我尝试访问登录"方法时,我得到一个空的响应,即使我输入了不正确的登录详细信息,它也是空的.

I have installed soapUI and setup our integration, it correctly receives the methods from the WSDL file but when I try to access the "login" method I get an empty response, even when I put in the incorrect login details it is empty.

soapUI 输出以下错误:

soapUI outputs the following error:

错误:发生错误[内容长度分隔的消息正文过早结束(预期:267;收到:266],详情参见错误日志

因此,http 标头似乎存在问题,某些函数能够返回响应(当然,如果没有登录哈希,它只是无效的,但至少它是一个响应).从我对 java 和 .net 的(极其有限的)理解来看,它们在这些方面比 php 严格得多,这说明了为什么 php 集成没有问题.

So it seems as though there is an issue with the http headers, some functions are able to return a response (of course without the login hash it is just invalid, but at least it is a response). From my (exceedingly limited) understanding of java and .net, they are much stricter on these things than php which would indicate why a php integration would have no issues.

谁能告诉我为什么会出现这个错误以及如何修复它?

Can anyone advise me why this error would occur and how to fix it?

推荐答案

我假设您使用的是符合 WS-I 的 SOAP API v2,因为我遇到了完全相同的问题;如果没有,那么这仍然可能适用.如果不符合 WS-I,我永远无法连接到 v2 API,这就是我发现此错误的方式.

I'm going to assume you are using the WS-I compliant SOAP API v2 as I had this exact same issue; if not, then this still might apply. I was never able to connect to the v2 API without it being WS-I compliant, which is how I found this bug.

如果您查看 app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php 文件,您将在公共运行函数中看到本质上是分为两部分 - 1 部分用于 WSDL 定义响应,1 部分用于实际 SOAP 响应.

If you have a look at the app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php file you will see in the public run function that is essentially split into two parts - 1 for the WSDL definition response and 1 for the actual SOAP response.

在 SOAP 响应中有一些字符串替换正在进行,用 <soap:operation soapAction=""></soap:operation> 替换 <soap:operation soapAction=""/> 等等.这显然会导致内容长度计算出现问题 - 这也发生在 WSDL 定义响应中,但似乎不会导致问题.

In the SOAP response there is a bit of string replacing going on, substituting <soap:operation soapAction=""></soap:operation> for <soap:operation soapAction="" /> and so on. This is evidently causing an issue with the content-length calculation - this is also occurring in the WSDL definition response, but it doesn't seem to be causing an issue.

通过用下面的代码替换 try 大括号之间的代码,我能够成功连接到 SOAP API.基本上,我最终清除了标题并重新计算了内容长度AFTER 字符串替换发生了:

I was able to successfully connect to the SOAP API by replacing the code between the try curly brackets with the code below. Basically, I ended up clearing the headers and recalculating the content-length AFTER the string replacement had taken place:

            $this->_instantiateServer();

            $content = preg_replace(
                        '/(\>\<)/i',
                        ">\n<",
                        str_replace(
                                '<soap:operation soapAction=""></soap:operation>',
                                "<soap:operation soapAction=\"\" />\n",
                                str_replace(
                                        '<soap:body use="literal"></soap:body>',
                                        "<soap:body use=\"literal\" />\n",
                                        preg_replace(
                                            '/<\?xml version="([^\"]+)"([^\>]+)>/i',
                                            '<?xml version="$1" encoding="'.$apiConfigCharset.'"?>',
                                            $this->_soap->handle()
                                        )
                                )
                        )
                    );

            $this->getController()->getResponse()
                      ->clearHeaders()
                      ->setHeader('Content-Type','text/xml; charset='.$apiConfigCharset)
                      ->setHeader('Content-Length',strlen($content))
                      ->setBody($content);

希望能帮到你

这篇关于Magento Soap API V2 响应内容长度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆