I have a WSDL that has an element that requires an attribute:
<xsd:complexType name="claim">
<xsd:annotation>
<xsd:documentation>Claim Element</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<!-- other elements removed -->
</xsd:sequence>
<xsd:attribute name="claimId" type="xsd:nonNegativeInteger" use="required" />
</xsd:complexType>
In terms of generated xml, it should look like:
<claims>
<claim claimId="1">
<!-- elements removed -->
</claim>
<!-- more claims -->
</claims>
Within a foreach
loop I am putting together an array of elements and using the attribute as part of the key:
//$claim = array of key/value pairs
$claim = [...];
$claim = new \SoapVar($claim, SOAP_ENC_OBJECT, null, null, 'claim claimId="' . ($key+1) . '"');
$claims['claim claimId="'.($key+1).'"'] = $claim;
When it comes to passing this to the SoapClient, the elements get removed:
//$client = new \SoapClient($wsdl);
$client->checkClaims($claims);
But all I'm getting is:
<claims />
How do I get my soap client to parse the claim
elements correctly in the soap call?
Knowing that
\SoapClient
can accept an xml string, I decided to convert my array of data into an xml string:This generates an xml string of:
Next step is to remove the attribute from the closing tags:
Finally, I wrap my xml in the appropriate parameter name and convert it to a
\SoapVar
so the\SoapClient
will parse it correctly:Passing
$claims
into my\SoapClient
and runningvar_dump($client->__getLastRequest());
shows my generated xml as:Just as it wanted.
So there are few issues with your code. For this to work, you need to use the SoapClient in WSDL mode (
$client = new \SoapClient($wsdl);
, which you are doing). Next below is wrongYou don't add attributes using
'claim claimId="' . ($key+1) . '"'
.Now what you need is to use a
classmap
. Below is a sample python flask app I created to show theWSDL
Then ran the same using
And ran another
socat
to view the trafficNext I wrote a sample PHP code to show how
classmap
worksAnd the resultant
xml
isReferences:
PHP soap request with an element attribute and child elements
PHP SoapVar Object Attribute?
Adding attributes to the actual function tag in PHP soapCall
How do I add additional attributes to XML Elements with the SoapClient Class in PHP
php SoapVar not setting attributes
Getting the XML as string for a SoapVar variable - without a webservice (locally)?
Getting the XML as string for a SoapVar variable - without a webservice (locally)?
http://fvue.nl/wiki/Php:_Soap:_How_to_add_attribute_to_SoapVar
https://forums.phpfreaks.com/topic/137357-solved-php-soap-client-node-attributes/
http://eosrei.net/articles/2012/01/php-soap-xml-attributes-namespaces-xmlwriter