Returning a PHP Array from a PHP SoapServer

Alan Storm picture Alan Storm · Jul 24, 2009 · Viewed 23.6k times · Source

I'm relatively new to Soap on the "creating the service side", so appologies in advance for any terminology I'm munging.

Is it possible to return a PHP array from a Remote Procedure Soap Service that's been setup using PHP's SoapServer Class?

I have a WSDL (built by blindly following a tutorial) that, in part, 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'm calling is named query, and looks something like this

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

This allows me to call

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

and dump of result will look something like

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

I want to return a native PHP array/collection from my query method. If I change my query method to return an array

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

It's serialized into an object on the client side.

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

This makes sense, as I've specific a xsd:object as the Result type in my WSDL. I'd like to, if possible, return an native PHP array that's not wrapped in an Object. My instincts say there's a specific xsd:type that will let me accomplish this, but I don't know. I'd also settle for the object being serialized as an ArrayObject.

Don't hold back on schooling me in the technical details os WSDL. I'm trying to get a grasp on the underlying concepts fo

Answer

lubosdz picture lubosdz · Oct 9, 2013

Little trick - encode as JSON objects, decode back into recursive associative arrays:

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