简体   繁体   English

PHP nusoap调用返回布尔值false而不是complexType数组

[英]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. 我正在尝试从php服务器获取复杂类型,并通过操作来获取动物数组(在wsdl中定义的复杂类型),该数组被定义为ConjuntAnimals作为Animal对象的数组。

But when I do the $result = $client->call('consulta_gossos', $params); 但是当我执行$result = $client->call('consulta_gossos', $params); I get returned a bool (false). 我返回了布尔值(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. 我已经调试和记录了很长时间,而且我已经看到在soap操作内部,就在返回之前,它的值是一个动物数组(应该返回什么),但是在客户端中我没有得到这回来了。

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... 在意识到它之后,我想也许是我错了的complexType定义,但是我已经与许多示例进行了比较,不应该这样。

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 我了解它期望xml响应,因此我尝试返回json_encode($gossos)而不是仅返回$gossos ,但它记录了以下内容: [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. 我正在返回一个数组,该数组由我称为structs的数组构成(对不起,我是PHP的新手,但仍然不知道语)是根据我从mysql查询中获取的内容构建的。 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. 我还返回了json编码(我真的不知道为什么,我想尝试一下...),现在我可以返回数组而不会遇到我遇到的那些错误。

Edit: Oh! 编辑:哦! And commented line 6132 in nusoap.php, without doing it, would still get errors! 并在nusoap.php中注释了6132行,如果不这样做,仍然会出错!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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