XMLRPC.Net 如何传入关联数组?

发布于 2024-10-21 01:34:34 字数 770 浏览 5 评论 0原文

我正在使用开源库 xmlrpc.net,并尝试调用一个具有关联数组输入参数的服务。

随叫随到的文档(我正在尝试与名为 Magento 的 php 站点集成,通过它抛出的错误,我知道它正在使用 Zend xmlrpc 库。)

方法名称: sales_order_shipment.create
为订单创建新的发货

返回:字符串 - 发货增量 id

参数:

字符串 orderIncrementId - 订单增量 id
array itemsQty - 作为关联数组运送的物品数量 (order_item_id ⇒ qty)
字符串注释 - 发货注释(可选)
布尔电子邮件 - 发送电子邮件标志(可选)
boolean includeComment - 在电子邮件标志中包含注释(可选)

所以在 .Net 中,我已经能够使以下内容正常工作

proxy.Create(sessionId, "sales_order_shipment.create", new object[] { 100000010, new object[] { }, "Shipment Created", true, true });

,但我似乎无法弄清楚我应该为 itemsQty< 传递什么 .Net 类型/代码>。 new object[]{} 可以工作,但我需要能够传入已发货的商品,而不仅仅是创建一个包含 0 件商品的发货。可以使用什么 .Net 类型来使用 xmlrpc.net 映射到关联数组

I am using the open source library xmlrpc.net and am trying to make a call to a service that has an input parameter that is an associative array.

Documentation on call (I am trying to integrate with a phpsite called Magento and by the errors it is throwing I know it is using the Zend xmlrpc library.)

Method Name: sales_order_shipment.create
Create new shipment for order

Return: string - shipment increment id

Arguments:

string orderIncrementId - order increment id
array itemsQty - items qty to ship as associative array (order_item_id ⇒ qty)
string comment - shipment comment (optional)
boolean email - send e-mail flag (optional)
boolean includeComment - include comment in e-mail flag (optional)

So In .Net I have been able to get the following to work

proxy.Create(sessionId, "sales_order_shipment.create", new object[] { 100000010, new object[] { }, "Shipment Created", true, true });

but I can't seem to figure out what .Net type I should pass in for itemsQty. new object[]{} works but I need to be able to pass in what items shipped not just create a shipment with 0 items shipping in it. What .Net type can be used that will map to an Associated Array using xmlrpc.net

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-10-28 01:34:34

如果没有看到应该生成的 XML-RPC 请求,规范对我来说并不清楚,但是像这样使用 XmlRpcStruct 怎么样:(

XmlRpcStruct items = new XmlRpcStruct();
items["orderid1"] = 1;
items["orderid2"] = 2;

假设订单 id 是一个字符串)

Without seeing the XML-RPC request that should be generated, the spec is not clear to me but how about using XmlRpcStruct like this:

XmlRpcStruct items = new XmlRpcStruct();
items["orderid1"] = 1;
items["orderid2"] = 2;

(assuming order id is a string)

顾忌 2024-10-28 01:34:34

我一直在使用相关的方法 sales_order_invoice.create 并遇到了与您相同的问题。我刚刚发现,无论出于何种原因,如果我在发票上添加注释,我都必须在传递给服务器的参数数组中插入一个额外的元素。

我正在运行 Magento EE 版本。 1.11.0.2 使用 C# 和 XML-RPC.Net v2 库 (CookComputing.XmlRpcV2.dll) 将数据与 Magento 集成。

我偶然发现了这个解决方案,因为注意到空发票上的注释是“0”,这是我为通过电子邮件发送发票(可选)字段提供的值,并决定尝试插入一个空发票评论之前的元素,评论出现了,但项目仍然没有开具发票。然后我将空元素移到项目列表之前,一切正常。我检查了 API /app/code/core/Mage/Sales/Model/Order/Invoice/Api.php 的代码,但找不到出现这种情况的位置或原因。我唯一的猜测是,这与解析 XML-RPC 请求的库没有得到正确的结果有关,因为此调用在其他参数中间有一个数组。

在尝试诊断此问题时,我使用了 XML-RPC 记录器

logger = new RequestResponseLogger();
logger.Directory = "C:\Temp\";
magentoProxy.AttachLogger(logger);
logger.UnsubscribeFrom(magentoProxy);

然后,每当我想查看调用中的请求响应是什么时,我只需将这些调用放在 XML-RPC 调用之前和之后,

logger.SubscribeTo(magentoProxy);
// call to Magento that I want to see the XML for request and responses to
logger.UnsubscribeFrom(magentoProxy);

我没有发现任何问题发送到 Magento 以进行 API 调用的 XML。我唯一能想到的另一件事是启动带有调试器的 magento,并观察当它到达 Api.php 文件中的 create 方法或在事情变得混乱之前的堆栈中时会发生什么,但我没有'当时我没有设置用于主动调试 Magento 代码的开发环境,并且现在不想花时间深入研究这方面的事情。

我所做的解决方法是在调用创建发票后添加一些代码,再次从 Magento 中提取 order_info 并检查订单上的所有项目是否已开具发票,如果没有,则会出现丑陋的情况错误。我认为,如果在某个时候这个“错误”或导致这种情况发生的任何事情得到修复或更改,我至少会知道它是否会影响从此通话中开具发票的订单项目。

        // Get the order items that need to be invoiced
        // this.orderInfo is the XmlRpcStruct returned from a sales_order.info call
        XmlRpcStruct[] orderItems = this.orderInfo.Contains("items") ? (XmlRpcStruct[]) this.orderInfo["items"] : new XmlRpcStruct[] { };

        XmlRpcStruct orderItemsToInvoice = new XmlRpcStruct();
        Int32 orderItemId;
        Int32 qtyOrdered;
        Int32 qtyInvoiced;
        Int32 qtyToInvoice;

        foreach (XmlRpcStruct item in orderItems)
        {
            orderItemId = item.Contains("item_id") ? Convert.ToInt32(item["item_id"]) : 0;
            qtyOrdered = item.Contains("qty_ordered") ? Convert.ToInt32(Convert.ToDecimal(item["qty_ordered"])) : 0;
            qtyInvoiced = item.Contains("qty_invoiced") ? Convert.ToInt32(Convert.ToDecimal(item["qty_invoiced"])) : 0;
            qtyToInvoice = qtyOrdered - qtyInvoiced;

            orderItemsToInvoice[Convert.ToString(orderItemId)] = Convert.ToString(qtyToInvoice);
        }

        // Invoice This Order with a comment
        String newInvoiceId = magentoProxy.salesOrderInvoiceCreate(sessionId: sessionId, arguments: new Object[] {
                this.MageIncrementId, // Order increment ID
                "", // this should not need to be here, but for some reason if I want to include a comment
                    // on the invoice I have to thave this extra empty element before the array of order items
                    // if no comment is included on the invoice this extra element is not needed, rather weird, I can not explain it.
                orderItemsToInvoice, // array    itemsQty    Array of orderItemIdQty (quantity of items to invoice)
                "Automatically invoiced prior to CounterPoint import." // Invoice Comment (optional)
                //"0", // Send invoice on email (optional) defaults to false
                //"0" // Include comments in email (optional) defaults to false
            });

        // Invoice This Order without a comment
        String newInvoiceId = magentoProxy.salesOrderInvoiceCreate(sessionId: sessionId, arguments: new Object[] {
                this.MageIncrementId, // Order increment ID
                orderItemsToInvoice // array    itemsQty    Array of orderItemIdQty (quantity of items to invoice)
            });

I've been playing with a related method sales_order_invoice.create and ran into the same problem you did. I just found that for whatever reason I had to insert an extra element into the array of arguments passed to the server if I included a comment on the invoice.

I'm running Magento EE ver. 1.11.0.2 using C# and the XML-RPC.Net v2 library (CookComputing.XmlRpcV2.dll) to integrate data with Magento.

I stumbled upon this resolution by noticing the comment on the empty invoices was "0", which was the value I was feeding for the Send invoice on email (optional) field, and decided to try inserting an empty element before the comment, and the comment showed up but the items were still not getting invoiced. I then moved the empty element before the list of items and everything worked. I checked the code for the API /app/code/core/Mage/Sales/Model/Order/Invoice/Api.php but couldn't find where or why this would be the case. My only guess is that it has something to do with the library that is parsing the XML-RPC request isn't getting something right since this call has an array in the middle of the other arguments.

In trying to diagnose this I've used the XML-RPC logger

logger = new RequestResponseLogger();
logger.Directory = "C:\Temp\";
magentoProxy.AttachLogger(logger);
logger.UnsubscribeFrom(magentoProxy);

Then anytime I want to see what the request responses are on a call I just put these calls before and after the XML-RPC call

logger.SubscribeTo(magentoProxy);
// call to Magento that I want to see the XML for request and responses to
logger.UnsubscribeFrom(magentoProxy);

I didn't see any problems with the XML that was sent to Magento for the API calls. The only other thing I could think of would be to launch magento with a debugger attached and watch what happens when it gets to that create method in the Api.php file or in the stack prior where things are getting messed up, but I didn't have my dev environment setup for active debugging of the Magento code at the time, and didn't want to spend the time digging into that aspect of things right now.

What I did as a work around was to add some code after the call to create the invoice that pulls down the order_info again from Magento and checks to see if all the items on the order have been invoiced, and if not it throws up an ugly error. I figure that way if at some point this "bug" or whatever is causing this to happen gets fixed or changes I'll at least know if it affects the order items getting invoiced from this call.

        // Get the order items that need to be invoiced
        // this.orderInfo is the XmlRpcStruct returned from a sales_order.info call
        XmlRpcStruct[] orderItems = this.orderInfo.Contains("items") ? (XmlRpcStruct[]) this.orderInfo["items"] : new XmlRpcStruct[] { };

        XmlRpcStruct orderItemsToInvoice = new XmlRpcStruct();
        Int32 orderItemId;
        Int32 qtyOrdered;
        Int32 qtyInvoiced;
        Int32 qtyToInvoice;

        foreach (XmlRpcStruct item in orderItems)
        {
            orderItemId = item.Contains("item_id") ? Convert.ToInt32(item["item_id"]) : 0;
            qtyOrdered = item.Contains("qty_ordered") ? Convert.ToInt32(Convert.ToDecimal(item["qty_ordered"])) : 0;
            qtyInvoiced = item.Contains("qty_invoiced") ? Convert.ToInt32(Convert.ToDecimal(item["qty_invoiced"])) : 0;
            qtyToInvoice = qtyOrdered - qtyInvoiced;

            orderItemsToInvoice[Convert.ToString(orderItemId)] = Convert.ToString(qtyToInvoice);
        }

        // Invoice This Order with a comment
        String newInvoiceId = magentoProxy.salesOrderInvoiceCreate(sessionId: sessionId, arguments: new Object[] {
                this.MageIncrementId, // Order increment ID
                "", // this should not need to be here, but for some reason if I want to include a comment
                    // on the invoice I have to thave this extra empty element before the array of order items
                    // if no comment is included on the invoice this extra element is not needed, rather weird, I can not explain it.
                orderItemsToInvoice, // array    itemsQty    Array of orderItemIdQty (quantity of items to invoice)
                "Automatically invoiced prior to CounterPoint import." // Invoice Comment (optional)
                //"0", // Send invoice on email (optional) defaults to false
                //"0" // Include comments in email (optional) defaults to false
            });

        // Invoice This Order without a comment
        String newInvoiceId = magentoProxy.salesOrderInvoiceCreate(sessionId: sessionId, arguments: new Object[] {
                this.MageIncrementId, // Order increment ID
                orderItemsToInvoice // array    itemsQty    Array of orderItemIdQty (quantity of items to invoice)
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文