繁体   English   中英

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

[英]XMLRPC.Net how do you pass in an associated array?

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

待命文档(我正在尝试与名为Magento的phpsite集成,由于抛出的错误,我知道它正在使用Zend xmlrpc库。)

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

返回:字符串-装运增量编号

参数:

字符串orderIncrementId-订单增量ID数组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映射到关联数组

在没有看到应该生成的XML-RPC请求的情况下,规范对我来说并不明确,但是如何像这样使用XmlRpcStruct:

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

(假设订单ID是一个字符串)

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

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

我偶然发现此空白决议,注意到空发票的注释为“ 0”,这是我为“ Send invoice on email (optional)字段提供的值,并决定尝试在注释之前插入一个空元素,然后评论出现了,但物品仍未开具发票。 然后,我将空元素移到了项目列表之前,一切正常。 我检查了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)
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM