简体   繁体   中英

PHP nusoap call returns bool false instead of complexType array

I'm trying to get a complex type from a php server with an operation to get an array of animals (complex type defined in the wsdl), this array is defined as ConjuntAnimals as an array of Animal objects.

But when I do the $result = $client->call('consulta_gossos', $params); I get returned a bool (false). I've been debugging and logging for a long time, and I've seen that inside the soap operation, just before returning, its value is an array of animals (what it should return), but in the client I don't get this returned.

After realizing it, I guess maybe it is the complexType definitions which I have wrong, but I've compared with a lot of examples and shouldn't be this...

Also, (maybe it helps) I get this error logged:

[10-Dec-2016 12:59:11 America/New_York] PHP Catchable fatal error: Object of class stdClass could not be converted to string in /home/cabox/workspace/lib/nusoap.php on line 6132 [10-Dec-2016 12:59:11 America/New_York] Response not of type text/xml: text/html

I understand it expects an xml response, so I've tried to return json_encode($gossos) instead of just $gossos , but then it logs this: [10-Dec-2016 13:11:34 America/New_York] XML error parsing SOAP payload on line 2: Invalid document end

Here I add both client and server code:

client:

<?php

  require_once('./lib/nusoap.php');

    $wsdl='http://php-ad-msk1416368101.codeanyapp.com/soapserver.php?wsdl';
    ini_set("log_errors", 1);
    ini_set("error_log", "/tmp/php-error.log");

  $client = new nusoap_client($wsdl,'wsdl'); 
    $client->encode_utf8 = false;
    $client->decode_utf8 = false;           
    $client->soap_defencoding = 'utf-8';

  $err = $client->getError();
    if ($err) {
        echo "Constructor error" . $err;
        exit();
    }

    $edat = $_GET['edat'];
    $raca = $_GET['raca'];
    $vacunat = $_GET['vacunes'];

    echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
    echo '<h2>Response</h2>';
    echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

    $result = $client->call('consulta_gossos', array('edat' => $edat, 'raca' => $raca, 'vacunat' => $vacunat));

    $err = $client->getError();
    error_log($err);
    error_log(json_encode($result));

    echo 'Type: '.gettype($result).', size: '.sizeOf($result);

?>

`

<?php
    require_once('./lib/nusoap.php');

    ini_set("log_errors", 1);
    ini_set("error_log", "/tmp/php-error.log");
    error_log( "Hello, errors!" );


    $server = new nusoap_server;
    $server->soap_defencoding = 'utf-8';
    $server->encode_utf8 = false;
    $server->decode_utf8 = false;
    $server->configureWSDL('server','urn:server');

    $server->wsdl->schemaTargetNamespace = 'urn:server';
    $server->wsdl->addComplexType('Animal',
                                                             'complexType',
                                                             'struct',
                                                             'all',
                                                             '',
                                                             array(
                                                                    'xip' => array('name' => 'xip', 'type' => 'xsd:int'),
                                                                    'nom' => array('name' => 'nom', 'type' => 'xsd:string'),
                                                                    'edat' => array('name' => 'edat', 'type' => 'xsd:int'),
                                                                    'vacunat' => array('name' => 'vacunat', 'type' => 'xsd:string'),
                                                                    'menjar' => array('name' => 'menjar', 'type' => 'xsd:string'),
                                                                    'data_entrada' => array('name' => 'data_entrada', 'type' => 'xsd:string'),
                                                                        )
                                                                    );

    $server->wsdl->addComplexType('ConjuntAnimals',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
            array('ref'=>'SOAP-ENC:arrayType',
                  'wsdl:arrayType'=>'tns:Animal[]')
    ),
    'tns:Animal'
    );
    $server->register('consulta_gossos',
                    array('edat' => 'xsd:string', 
                                'raca' => 'xsd:string', 
                                'vacunat' => 'xsd:string'),
                    array('return' => 'tns:ConjuntAnimals'),    //output
                    'urn:server',                               //namespace
                    'urn:server#consulta_gossos',               //SOAP action
                    'rpc',
                    'encoded',
                    'Consultar els gossos que compleixen el filtre');//description

    function consulta_gossos($edat, $raca, $vacunat) {
        $servername = 'localhost';
        $username = 'root';
        $password = 'root';
        $dbname = 'bd_refugi';

        $vacunes = ($vacunat =='si') ? 1 : 0;
        $sql_edat = ($edat == 'gran') ? ' edat > 5 ' : ' edat <= 5 ';

        $connection = new mysqli($servername, $username, $password, $dbname);
        $sql = 'select * from gossos where '.$sql_edat.' and raca="'.$raca.'" and vacunat='.$vacunes;
        $res = $connection->query($sql);

        $gossos = array();
        while ($row=$res->fetch_assoc()) {
                $gos->xip=$row['xip'];
                $gos->nom=$row['nom'];
                $gos->edat=$row['edat'];
                $gos->vacunat=$row['vacunat'];
                $gos->menjar=$row['menjar'];
                $gos->data_entrada=$row['data_entrada'];
                array_push($gossos,$gos);
        }
        error_log(json_encode($gossos));
        error_log('-----------------------------------------');
        return json_encode($gossos);

    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);

    ?>

`

Sorry for the indenting, my eyes are bleeding too after copy-pasting the code here.

Edit: leaving aside the errors shown above, the question intends to focus more on why returning a "correct" array, in the client I get an empty array once I've get rid off all these errors.

After several hours of debugging and testing each type individually, I've finally managed to solve this.

I was returning an array composed by what I would call structs (sorry I'm new to PHP, still don't know the slang) build from what I was getting from the mysql query. The thing is that they should have been arrays, so I add the code I've changed:

Before (not working):

$res = $connection->query($sql);

    $gossos = array();
    while ($row=$res->fetch_assoc()) {
            $gos->xip=$row['xip'];
            $gos->nom=$row['nom'];
            $gos->edat=$row['edat'];
            $gos->vacunat=$row['vacunat'];
            $gos->menjar=$row['menjar'];
            $gos->data_entrada=$row['data_entrada'];
            array_push($gossos,$gos);
    }
    return json_encode($gossos);

After (working):

$res = $connection->query($sql);
    $gossos = array();
    while ($row=$res->fetch_assoc()) {
        $gos = array('xip' => intval($row['xip']), 
                                 'nom' => $row['nom'], 
                                 'edat' => intval($row['edat']), 
                                 'vacunat' => $row['vacunat'], 
                                 'menjar' => $row['menjar'], 
                                 'data_entrada' => $row['data_entrada'],
                                 'sexe' => $row['sexe'],
                                 'raca' => $row['raca']
                                );
        array_push($gossos,$row);
    }
    return $gossos;

I also was returning the json encoding (I really don't know why, I guess for trying things...), now I can return the array without getting those errors I had.

Edit: Oh! And commented line 6132 in nusoap.php, without doing it, would still get errors!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM