views:

107

answers:

1

I'm building a simple WebService. I have a User PHP class and a webmethod getUser to retrieve a user's information.

I've declared a complexType and everything seems to work perfectly.

$server->wsdl->addComplexType('User',
    'complexType','struct', 'all', '',
    array(  'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'username' => array('name' => 'username','type' => 'xsd:string'),
            'password' => array('name' => 'password','type' => 'xsd:string'),
            'email' => array('name' => 'email','type' => 'xsd:string'),
            'authority' => array('name' => 'authority','type' => 'xsd:int'),
            'isActive' => array('name' => 'isActive','type' => 'xsd:int'),
            'area' => array('name' => 'area','type' => 'xsd:string')
    )
);
$server->register('ws_getUser',
    array('user_id' => 'xsd:integer'),
    array('user' =>  'tns:User'),
    $namespace,
    "$namespace#ws_getUser",
    'rpc',
    'encoded',
    'Retorna un usuario'
);

function ws_getUser($user_id){
    return new soapval('return', 'tns:User', getUser($user_id));
}

However, on the getUser function I'm retrieving the user info as an assoc. array, not the User Object itself.

What I would like to do on getUser is to return a User instance instead, and have nuSOAP serialize it for me. Is this possible?

Edit: I've tried returning a new User() for testing purposes, but the response is

<user xsi:type="tns:User"/>
A: 

Guess I've found out an answer that may apply to this case here: http://www.php.net/manual/en/class.soapserver.php

If you want to return a custom object array from a nusoap webservice, you have to cast the objects to arrays like so:

<?php
$users = array();
while($res = $db_obj->fetch_row())
{
  $user = new user();
  $user->Id = $res['id'];
  $user->Username = $res['username'];
  $user->Email = $res['email'];

  $users[] = (array) $user;
}

return ($users);
ign