返回数组的数组nusoap php web服务

时间:2013-01-17 14:03:18

标签: php mysql arrays nusoap

我在返回数组数组时出现问题,我使用nusoap在php中创建了一个Web服务,这是我的代码:

server.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);
?>

client.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/>"; 
        }
 ?>

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

对不起我的英语

1 个答案:

答案 0 :(得分: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] => 
)

希望这有帮助。