Sunday, 2 November 2014

PHP Soapclient xsi:type in response



I'm calling a method in a SOAP server, and the response includes the following XML tag:



<SelctedSupplements>
<Supplement xsi:type="PerRoomSupplement" suppId="1000680" suppName="Upgrade" supptType="4" suppIsMandatory="true" suppChargeType="Included" price="0.00" publishPrice="0.00"/>
</SelctedSupplements>


The result I'm getting is the following:



object(stdClass)#156 (1) {
["Supplement"]=>
object(stdClass)#157 (7) {
["suppId"]=> int(1000680)
["suppName"]=> string(7) "Upgrade"
["supptType"]=> int(4)
["suppIsMandatory"]=> bool(true)
["suppChargeType"]=> string(8) "Included"
["price"]=> string(4) "0.00"
["publishPrice"]=> string(4) "0.00"
}
}


So all of the attributes are there, except for xsi:type="PerRoomSupplement". How do I get xsi:type of the Supplement tag? Do I have to get the last XML response and parse it through an XML library?




Before I submitted the question, I did some googling, and found a way to accomplish what I want, it may not be the ideal answer, but it does what I want. Here it is in case someone else is struggling with the same issue:


I know that the possible types of the Supplement tag are either PerRoomSupplement or PerPersonSupplement. What I did is create two classes with those names, and then map the SOAP response classes to my PHP classes. This way, I can easily know which type the supplement is (by using the get_class function)



class PerRoomSupplement{}
class PerPersonSupplement{}
$client = new SoapClient($url, array("classmap" => array(
"PerRoomSupplement" => "PerRoomSupplement",
"PerPersonSupplement" => "PerPersonSupplement"
)));




Thanks,


No comments:

Post a Comment