Passing custom types in PHP SOAP

I am having trouble understanding how to pass custom types in PHP SOAP calls. Can someone give me a hint (or a link to a guide), please?

Example: In my WSDL file, I define the type:

<types> <schema targetNamespace="http://example.com/CustData" xmlns="http://www.w3.org/2000/10/XMLSchema"> <element name="personalInformation"> <complexType> <all> <element name="name" type="xsd:string"/> <element name="title" type="xsd:string"/> <element name="lang" type="xsd:string"/> </all> </complexType> </element> </schema> 

I define the service response message as follows:

 <message name='getCustDataResponse'> <part name='Result' type='xsd:personalInformation'/> <part name='Result1' type='xsd:string'/> </message> 

Invalid part - how to initialize the response on the SOAP server side?

I tried to write:

 $arrRes['Result']['name'] = 'xxx'; $arrRes['Result']['title'] = 'yyy'; $arrRes['Result']['lang'] = 'zzz'; $arrRes['Result']['hehehehe1'] = 'test1'; $arrRes['Result']['hehehehe2'] = 'test2'; $arrRes['Result']['hehehehe3'] = 'test3'; $arrRes['Result']['hehehehe4'] = 'test4'; $arrRes['Result1'] = 'result1'; $arrRes['blablabla'] = 'hahaha'; return $arrRes; 

The client gets the response back, and when I var_dump it, it shows arrRes:

 array(2) { ["Result"]=> array(7) { ["name"]=> string(3) "xxx" ["title"]=> string(3) "yyy" ["lang"]=> string(3) "zzz" ["hehehehe1"]=> string(5) "test1" ["hehehehe2"]=> string(5) "test2" ["hehehehe3"]=> string(5) "test3" ["hehehehe4"]=> string(5) "test4" } ["Result1"]=> string(7) "result1" } 

I expected to get an error because the initialized array does not match the response message that I defined.

So, I assume that the type that I defined in wsdl is not used at all, so it should be an error either in wsdl or in the client or server code.

Thanks in advance for your advice!

Nikola

+4
source share
1 answer

I haven’t done much with the server side of SOAP in php, but here is an example of using class mapping with php SoapClient. I'm sure SoapServer works the exact same way. (Perhaps you can even use the same class map between the server / client).

So you will have a class like this:

 class PersonalInformation { public $name; public $title: public $lang; } 

Then for your answer:

 function getCustData() { $response = new PersonalInformation; $response->name = "Me"; $response->title = "Hi World"; $response->lang = "En-US"; $arrResult = array(); $arrResult['Result'] = $response; $arrResult['Result1'] = 'lol'; return $arrResult } 

Then just use the class map, for example:

 $server = new SoapServer('foo?wsdl', classmap=array('personalInformation' => 'PersonalInformation')); //I'm not sure whether you have to use the classmap on BOTH server/client $client = new SoapClient('foo?wsdl', classmap=array('personalInformation' => 'PersonalInformation')); 

As for errors in inappropriate response data, I don't think php really does any kind of check for the answer - just a request.

+2
source

Source: https://habr.com/ru/post/1302042/


All Articles