PHP array return from PHP SoapServer

I am relatively new to Soap on “creating a service part”, so I propose any terminology that I collect in advance.

Is it possible to return a PHP array from a remote procedure processing service that was configured using the PHP class SoapServer?

I have a WSDL (built to blindly follow a tutorial) which, in particular, looks something like this:

<message name='genericString'>
    <part name='Result' type='xsd:string'/>
</message>

<message name='genericObject'>
    <part name='Result' type='xsd:object'/>
</message>

<portType name='FtaPortType'>       
    <operation name='query'>
        <input message='tns:genericString'/>
        <output message='tns:genericObject'/>
    </operation>        
</portType>

The PHP method I call is called a query and looks something like this.

public function query($arg){
    $object = new stdClass();
    $object->testing = $arg;
    return $object;     
}

It allows me to call

$client = new SoapClient("http://example.com/my.wsdl");
$result = $client->query('This is a test');

and the result dump will look something like

object(stdClass)[2]
    public 'result' => string 'This is a test' (length=18)

I want to return my own PHP array / set from my request method. If I change my request method to return an array

public function query($arg) {
    $object = array('test','again');
    return $object;
}

It serializes to an object on the client side.

object(stdClass)[2]
    public 'item' => 
        array
            0 => string 'test' (length=4)
            1 => string 'again' (length=5)

, xsd:object WSDL. , , PHP-, Object. , xsd:, , . , ArrayObject.

WSDL. fo

+3
3

WSDL .

- , -, WSDL:

<wsdl:types>
<xsd:schema targetNamespace="http://schema.example.com">
  <xsd:complexType name="stringArray">
    <xsd:complexContent>
      <xsd:restriction base="SOAP-ENC:Array">
        <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]" />
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

</wsdl:types>
<message name="notifyRequest">
  <part name="parameters" type="xsd:string" />
</message>
<message name="notifyResponse">
  <part name="notifyReturn" type="tns:stringArray" />
</message>

API notify:

<wsdl:operation name="notify">
  <wsdl:input message="tns:notifyRequest" />
  <wsdl:output message="tns:notifyResponse" />
</wsdl:operation>
+2

- JSON, :

$data = json_decode(json_encode($data), true);
+5

Alan, why not pass your object as an array when your client receives a response?

eg.

(array) $object;

This will convert your stdClass object into an array, there is no measurable overhead for this and O (1) in PHP.

You might want to try changing the type from xsd: object to soap-enc: Array.

0
source

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


All Articles