1

我在返回数组数组时遇到问题,我使用 nusoap 在 php 中创建 Web 服务,这是我的代码:

服务器.php

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

    $miURL = 'http://localhost/webservice/';
    $server = new soap_server();
    $server->configureWSDL('Web Service de Pruebas', $miURL);
    $server->wsdl->schemaTargetNamespace = $miURL;

    $server->wsdl->addComplexType(
            'array_php',
            'complexType',
            'struct',
            'all',
            '',
                array(
                    'pk' => array('name' => 'pk', 'type' =>'xsd:int'),
                    'rol' => array('name' => 'rol', 'type' =>'xsd:string'),
                    'descripcion' => array('name' => 'descripcion', 'type' =>'xsd:string')
                )
            );

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

    $server->register('prueba',                    // method name
        array(),                            // input parameters
        array('return' => 'tns:array_array'),    // output parameters
        $miURL,                         // namespace
        $miURL . '#prueba',                   // soapaction
        'rpc',                                    // style
        'encoded',                                // use
        'Get Specific array_php'        // documentation
      );

    function prueba()
    {
        $con = mysql_connect('localhost', 'root', '1234');
        mysql_selectdb('laboral', $con);

        $sql = "SELECT * FROM roles";
        $q = mysql_query($sql);

        $item = array();
        while($r = mysql_fetch_assoc($q)){
            $item[] = $r;
        }
        return $item;

    }


    if( !isset($HTTP_RAW_POST_DATA) )
        $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );

    $server->service($HTTP_RAW_POST_DATA);
?>

客户端.php

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

    //lineas de configuracion
    $serverURL = 'http://localhost/webservice/ws2.php?wsdl';

    $cliente = new nusoap_client($serverURL);

    //sí error de conexión:
    $error = $cliente->getError();
    if($error){
        echo "<p> '.$error.' </p>";
        echo '<p style="color:red;'>htmlspecialchars($cliente->getDebug(), ENT_QUOTES).'</p>';
        die();
    }


    echo "<br/>";
    $arreglo2 = $cliente->call('prueba');

    echo "<br/>";
    for( $i=0; $i<3; $i++ ){
            print_r( $arreglo2[$i]['pk'] );
            print_r( $arreglo2[$i]['rol'] );
            print_r( $arreglo2[$i]['descripcion'] );
            echo "<br/>"; 
        }
 ?>

问题是退货,没有向我的客户返回任何东西,我不知道会发生什么,我一直在阅读很多论坛但我找不到答案,请如果有人知道这里帮助我

ty,对不起我的英语

4

1 回答 1

2

这就是我所做的:

// Complex Array Keys and Types ++++++++++++++++++++++++++++++++++++++++++
$server->wsdl->addComplexType('notaryConnectionData','complexType','struct','all','',
        array(
                'id' => array('name'=>'id','type'=>'xsd:int'),
                'name' => array('name'=>'name','type'=>'xsd:string')
        )
);
// *************************************************************************

// Complex Array ++++++++++++++++++++++++++++++++++++++++++
$server->wsdl->addComplexType('notaryConnectionArray','complexType','array','','SOAP-ENC:Array',
        array(),
        array(
            array(
                'ref' => 'SOAP-ENC:arrayType',
                'wsdl:arrayType' => 'tns:notaryConnectionData[]'
            )
        )
);
// *************************************************************************

// This is where I register my method and use the notaryConnectionArray
$server->register("listNotaryConnections",
                array('token' => 'xsd:string'),
                array('result' => 'xsd:bool', 'notary_array' => 'tns:notaryConnectionArray', 'error' => 'xsd:string'),
                'urn:closingorder',
                'urn:closingorder#listNotaryConnections',
                'rpc',
                'encoded',
                'Use this service to list notaries connected to the signed-in title company.');

// In my function, I query the data and do:
$list = array();
$results = mysql_query($query);
while($row = mysql_fetch_assoc($results)) {
    array_push($list, array('id' => intval($row['na_id']), 'name' => $row['agency_name']));
}

return array("result" => true, "notary_array" => $list);

// The output is:
Array
(
    [result] => 1
    [notary_array] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Agency 1
                )

            [1] => Array
                (
                    [id] => 3
                    [name] => Agency 3
                )

            [2] => Array
                (
                    [id] => 4
                    [name] => Agency 4
                )

        )

    [error] => 
)

希望这可以帮助。

于 2014-09-23T02:06:25.260 回答