Simon Muñoz
Simon Muñoz

Reputation: 11

return array of arrays nusoap php web service

i have a problem returning an array of arrays, i making a web service in php using nusoap, here is my code:

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

the problem is the return, is returning nothing to my client and i dont know what happens, i been reading a lot of forums but i cant find a answer, please if some one know about help me here

ty and sorry for my english

Upvotes: 1

Views: 8201

Answers (1)

James
James

Reputation: 3805

This is what I did:

// 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] => 
)

Hope this helps.

Upvotes: 2

Related Questions