how to deal with array ComplexType in NuSoap?

14.5k Views Asked by At

currently i am using PHP Version 5.4.16, and NuSoap 0.95, i have try this example : here

which shown the server code like this ;

$server->wsdl->addComplexType(
    'Chapter',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'title' => array('name'=>'title','type'=>'xsd:string'),
        'page' => array('name'=>'page','type'=>'xsd:int')
    )
);

$server->wsdl->addComplexType(
    'ChapterArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Chapter[]')
    ),
    'tns:Chapter'
);

$server->wsdl->addComplexType(
    'Book',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'author' => array('name'=>'author','type'=>'xsd:string'),
        'title' => array('name'=>'title','type'=>'xsd:string'),
        'numpages' => array('name'=>'numpages','type'=>'xsd:int'),
        'toc' => array('name'=>'toc','type'=>'tns:ChapterArray')
    )
);

$server->register(
    'getBook',
    array('title'=>'xsd:string'),
    array('return'=>'tns:Book'),
    $NAMESPACE);

function getBook($title) {
     // Create TOC
     $toc = array();
     $toc[] = array('title' => 'Chapter One', 'page' => 1);
     $toc[] = array('title' => 'Chapter Two', 'page' => 30);

     // Create book
     $book = array(
                 'author' => "Jack London",
                 'title' => $title,
                 'numpages' => 42,
                 'toc' => $toc);

     return $book;
}

but got error returned at the SOAP Response, i am testing it using SopUI. i got an error like this :

<br />
<b>Notice</b>:  Array to string conversion in <b>E:\xampp\htdocs\myweb\lib\nusoap.php</b> on line <b>6132</b><br />
<br />
<b>Notice</b>:  Array to string conversion in <b>E:\xampp\htdocs\myweb\lib\nusoap.php</b> on line <b>6132</b><br />
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getBookResponse xmlns:ns1="http://localhost/myweb"><return><author>Jack London</author><title>This is my book</title><numpages>42</numpages><toc><item><title>Chapter One</title><page>1</page></item><item><title>Chapter Two</title><page>30</page></item></toc></return></ns1:getBookResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

how can i fix this error? can somebody help me to solve this error please... many thanks in advance...

3

There are 3 best solutions below

3
On

In nusoap > lib > nusoap.php, comment line 6132:

////$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
0
On

A neater solution than commenting the line 6132 in nusoap > lib > nusoap.php would be to check if $v is an array and convert it to string:

current line 6132:

$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");

change to:

$this->debug("serializing array element: $k, " . ( is_array($v) ? join(',', $v) : $v ) . " of type: $typeDef[arrayType]");
1
On
$server->wsdl->addComplexType(
    'Book',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'author' => array('name1'=>'author','type'=>'xsd:string'),
        'title' => array('name2'=>'title','type'=>'xsd:string'),
        'numpages' => array('name3'=>'numpages','type'=>'xsd:int'),
        'toc' => array('name4'=>'toc','type'=>'tns:ChapterArray')
    )
);

$server->register(
    'getBook',
    array('title'=>'xsd:string'),
    array('return'=>'xsd:array'),
    $NAMESPACE);