Search code examples
magentomagento-soap-api

Magento API SOAP v1 and VB.NET


I'm using VB.NET to communicate with Magento via the API SOAP v1. I had it working fine until I got to a call that needed an associative array. I've tried dictionaries, hashtables, custom types, etc. I did read hashtables and dictionaries are not serializable. So I tried the custom type/object/class. Below is the error I received:

The type XXXX was not expected. Use the Xmlinclude or SoapInclude attribute to specify types that are not known statically.

So I've read a lot of posts in regards to the above error and I can't get anything to work. The error seems at least to say what I'm trying is possible if I do it right, but maybe that is not the case. I don't know much about SOAP, but I can see this never working since the web service might not know how to handle the object.

So my question is if it's possible to send a custom object to the Magento API. Or more broadly is it possible to get SOAP v1 to work with VB.NET. I know v2 is an option but I'm pretty familiar with v1 and already having it working in VB.NET other than this scenario.


Solution

  • It took a lot of digging, but I have it all working. To start I used the PHP SoapClient and noted how it formed associative arrays in the soap request. You can trace requests and responses, pretty handy. From there I wrote my own soap client in VB.NET using a WebRequest object. In doing so I have full control over the xml being sent to the API.

    Again the reason I went down this road is ultimately V2 was not working for me. For some reason not all parameters were making it to the API. That and the fact I'm pretty comfortable with V1 too. I've written several custom APIs.

    I apologize for the brevity, but there is a lot that went into this. Probably most of my time was hitting multiple dead ends. If anyone wants specifics feel free email me.

    EDIT:

    Here is the php code I used to see how I needed to format the requests:

    $client = new SoapClient('http://www.site.com/index.php/api/soap/?wsdl',array('trace' => TRUE));
    $session = $client->login('user','api-key');
    
    echo $client->__getLastRequest() ."\n\n";
    echo $client->__getLastRequestHeaders() ."\n\n";
    echo $client->__getLastResponse() ."\n\n";
    echo $client->__getLastResponseHeaders() ."\n\n";
    
    $result = $client->call($session, 'cataloginventory_stock_item.list','393');
    
    echo $client->__getLastRequest() ."\n\n";
    echo $client->__getLastRequestHeaders() ."\n\n";
    
    var_dump($result);
    $client->endSession($session);
    

    Below is how to send the request using VB.NET. You will need to construct the XML/SOAP body using the php above as a guide. I made a class per API call which output the needed XML. You will need System.Net, System.Xml & System.IO. I used getSoapHeader() because there is some common XML that goes into a request. See next code section for more details:

        Private Function makeSoapRequest(ByVal soapBody As String) As String
    
        Dim req As WebRequest = WebRequest.Create(_soap_url)
        Dim xml As String
    
        xml = getSoapHeader() & soapBody
    
        Dim buffer() As Byte = System.Text.Encoding.UTF8.GetBytes(xml)
        req.ContentType = "text/xml; charset=utf-8"
        req.Method = "POST"
    
        req.Headers.Add("SOAPAction", "urn:Mage_Api_Model_Server_HandlerAction")
        req.ContentLength = buffer.Length
    
        Dim st As System.IO.Stream = req.GetRequestStream
    
        st.Write(buffer, 0, buffer.Length)
        st.Close()
    
        Dim response As WebResponse
    
        Try
            response = req.GetResponse
        Catch ex As WebException
            response = ex.Response
        End Try
    
        st = response.GetResponseStream()
    
        Dim reader As New StreamReader(st)
    
        Dim responseFromServer As String = reader.ReadToEnd()
    
        makeSoapRequest = responseFromServer
    
        response.Close()
        st.Close()
    
    End Function
    

    Below is the getSoapHeader() function. As noted the ns2 portion is only needed if you are using type="ns2:Map" which is what I needed for associative arrays:

        Private Function getSoapHeader() As String
        'ns2 is not always needed
        getSoapHeader = "<?xml version=""1.0"" encoding=""UTF-8""?><SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns1=""urn:Magento"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:ns2=""http://xml.apache.org/xml-soap"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> " & vbCrLf
    
    End Function