2011-03-08 16 views
0

我正在使用開源庫xmlrpc.net,並試圖調用具有輸入參數爲關聯數組的服務。XMLRPC.Net你如何傳入相關數組?

隨叫隨到文檔(我試圖用一種叫做Magento的phpsite整合和錯誤是扔我知道它使用Zend XMLRPC庫)。

方法名稱:sales_order_shipment.create 創建新貨訂單

返回:字符串 - 出貨增量ID

參數:

串orderIncrementId - 階增量ID ARRA ŸitemsQty - 項目數量出貨爲關聯數組(ORDER_ITEM_ID⇒數量) 字符串評論 - 發貨註釋(可選) 布爾電子郵件 - 發送電子郵件標誌(可選) 布爾includeComment - 包括電子郵件標誌(可選)評論

所以在.NET中我已經能夠得到以下工作

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

,但我似乎無法找出.NET類型,我應該通過在itemsQty。新的對象[] {}的作品,但我需要能夠通過什麼項目出貨,不只是創建一個與其中0項出貨的貨物。可以使用什麼.Net類型映射到使用xmlrpc.net的關聯陣列

回答

2

沒有看到應該生成的XML-RPC請求,該規範對我來說並不清楚,但如何使用像這樣的XmlRpcStruct:

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

(假設訂單ID是一個字符串)

1

我一直在玩一個相關的方法sales_order_invoice.create跑進你做了同樣的問題。我只是發現,無論出於何種原因,如果我在發票上添加了評論,我必須在傳遞給服務器的參數數組中插入額外的元素。

我正在運行Magento EE ver。 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文件或堆棧中創建方法時會發生什麼情況,但事先會出現混亂的情況,沒有我的開發環境設置,用於當時對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) 
      }); 
+0

謝謝 - XmlRpcStruct真的很有用! – Eleasar

+0

Magento EE 1.13修復了很多API調用的bug。我還發現,我能夠輕鬆地獲得一個SOAP示例以使用EE 1.13,所以有一種使用XML-RPC的替代方案。我使用SOAP和EE 1.13做了一個概念證明項目(使用禮品卡),並且代碼更加簡單。我希望將XML-RPC轉換爲SOAP,以便繼續進行Magento API調用。 – mttjohnson