Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing user-defined types in PHP SOAP

Tags:

types

php

soap

I'm having trouble understanding how to pass user-defined types in PHP SOAP calls. Could someone give me a hint (or a link to a manual) 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 like this:

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

The missing part is - how do I initialize the answer on the SOAP server side?

I tried writing:

$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 the 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 array I initialized doesn't match the response message I have defined.

So I guess the type I have defined in the wsdl isn't used at all - so it must be an error either in the wsdl or in the client or server code.

Thanks in advance for your advice!!

Nikola

like image 582
Nikola Avatar asked Nov 06 '22 16:11

Nikola


1 Answers

I haven't done much with the server-side of SOAP on php, but here is an example of how to use class mapping with php's SoapClient. I am fairly certain that SoapServer works the exact same way. (You could probably even share the same class map between server/client).

So you would have a class like this:

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

Then for your response:

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 a class map like:

$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 far as the errors on non-conforming response data, I don't think php really does any validation on the reponse - only the request.

like image 111
Kayla Rose Avatar answered Nov 14 '22 22:11

Kayla Rose