Page 1 of 1

SOAP Client and Array problem

Posted: Tue Mar 16, 2010 6:55 pm
by Sofw_Arch_Dev
Folks,

Has anyone had to deal with sending arrays of complex XML elements as part of a SOAP message?

I'm attempting to use PHP's SoapClient to call a function on a remote server. The server's XML schema specifies the input type to the function as taking a complex type comprised of a couple elements. One of the elements is an array element, which encapsulates an unbounded array of another particular complex type.

My issue is that whenever I attempt to create an array of elements of the inner type, I get empty elements. I create a PHP stdObject to encapsulate the SOAP message as such:

Code: Select all

 
$container1->AnswerContainer->Answer = "answer1";
$container2->AnswerContainer->Answer = "answer2";
 
$soapMsg->Answers = array( $container1, $container2 );
$soapMsg->QuestionId = 1;
 
Yet I get the following in my SOAP request:

Code: Select all

 
<ns1:QuestionId>1</ns1:QuestionId>
<ns1:Answers>
        <ns1:AnswerContainer />
        <ns1:AnswerContainer />
</ns1:Answers>
 
The above XML shows that the AnswerContainer element is empty. It should have an Answer element in each one. Going back to the code, if I have only one AnswerContainer in the array, the same thing happens just with one empty AnswerContainer element. If I create just one AnswerContainer with no array and set Answers to just a single AnswerContainer, everything looks as expected and the SOAP call goes through.

Code: Select all

 
<ns1:QuestionId>1</ns1:QuestionId>
<ns1:Answers>
        <ns1:AnswerContainer>
            <ns1:Answer>answer1</ns1:Answer>
        </ns1:AnswerContainer>
</ns1:Answers>
 
I can't control the schema, but I've poured over documentation and can verify that the schema defines the array properly. I've tried using SoapVars instead, having found some cool suggestions on PHP.net, but SoapVars only introduce other problems specifically related to the array of AnswerContainer.

Has anyone encountered this issue before? Is this a shortcoming with PHP's SoapClient? I find it unlikely that PHP wouldn't be able to handle an array of elements, and I really don't want to pull out Java to do this. Seems like Java would be way to much of a headache for such a simple task.

Any ideas?

Re: SOAP Client and Array problem

Posted: Tue Mar 16, 2010 7:19 pm
by Weirdan
You forgot to post the xsd fragment defining the Answers type. It was a long time since I worked with SOAP in PHP but I do remember having quite a lot of problems with xsd types -> php types mappings specifically. Back then I developed a script to build xsd definitions from php types/classes (can't share it though since it was for a proprietary project), but if you posted the xsd I'd probably be able to find out what php types it would correspond to.

Re: SOAP Client and Array problem

Posted: Wed Mar 17, 2010 1:13 pm
by Sofw_Arch_Dev
Thank you Weirdan,

here's the xsd.

Code: Select all

 
<s:element name="SurveyResponseInsert">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="QuestionId" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="Answers" type="tns:ArrayOfAnswerContainer" />
        </s:sequence>
    </s:complexType>
</s:element>
 
 <s:complexType name="ArrayOfAnswerContainer">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="unbounded" name="AnswerContainer" type="tns:AnswerContainer" />
    </s:sequence>
 </s:complexType>
      
<s:complexType name="AnswerContainer">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="Answer" type="s:string" />
    </s:sequence>
</s:complexType>
 
You'll immediately notice that they're not using complexContent for the array container, but a sequence instead. Seems legit, though I would have thought the array was defined differently using complexContent and a restriction on a base type. I've tried using my own local version of the wsdl using a different definition of the array type, but to no avail.

It'd be great if there was an issue with the schema/wsdl. That'd be easy enough to request a change to. I appreciate your insight.