Error returning array of complex types

Help
2008-10-06
2013-06-06
  • Phil Douglas
    Phil Douglas
    2008-10-06

    Hey all

    I am unable to return an array of complex types, my webservice returns the following error message in its response:

    <b>Catchable fatal error</b>:  Object of class Subject could not be converted to string in <b>C:\xampp\htdocs\flashexam\api\lib\nusoap.php</b> on line <b>6002</b><br />

    My function is defined like so:

        $server->register('getSubjects',
            array(),                                                        // input parameters
            array('success' => 'xsd:boolean', 'message' => 'xsd:string', 'subjects' => 'tns:SubjectArray'),// output parameter
            $ns,                                                             // namespace
            "$ns#ws_add",                                                        // soapaction
            'rpc',                                                                          // style
            'encoded',                                                                  // use
            'Get subjects'                                                           // documentation
        );

    And my types are defined like this:

        $server->wsdl->addComplexType(
            'Subject',
            'complexType',
            'struct',
            'all',
            '',
            array(
                'id' => array('name' => 'id', 'type' => 'xsd:int'),
                'name' => array('name' => 'name', 'type' => 'xsd:string')
            )
        );
       
        $server->wsdl->addComplexType(
            'SubjectArray',
            'complexType',
            'array',
            '',
            'SOAP-ENC:Array',
            array(),
            array(
                array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Subject[]')
            ),
            'tns:Subject'
        );

    I have another function defined that returns a single "Subject" object and that works fine:

        $server->register('getSubject',
            array('id' => 'xsd:int'),                                                    // input parameters
            array('success' => 'xsd:boolean', 'message' => 'xsd:string', 'subject' => 'tns:Subject'),    // output parameter
            $ns,                                                                     // namespace
            "$ns#ws_add",                                                                // soapaction
            'rpc',                                                                                  // style
            'encoded',                                                                          // use
            'Get specified subject'                                                           // documentation
        );

    Also the getSubjects function that is being registered here works fine on its own. The problem seems to be the way i'm registering that function, but i've followed a variety of different examples and mine looks like it should work.

    I've searched for a solution to this, but can't find one. I'm having the same problem with all the arrays of complex types i try and return. Any help would be greatly appreciated.

    Cheers

     
    • Phil Douglas
      Phil Douglas
      2008-10-06

      I found a solution elsewhere on the net. I needed to add:

      function __toString() {
              return $this->name;
          }

      to my Subject class.

      Is this a bug with nusoap?