phpweb-servicessoapnusoap

How to return nested array using Nusoap complextype in PHP?


I am writing a web service in php using nusoap library. I can return simple variables such as string, integer and also only one complextype which is struct. But I can not return array of structs. I research nearly all example in a week about this but it doesn't work. So where I am going wrong?

Code

$server->wsdl->addComplexType('userSitesData','complexType','struct','all','',
        array(
                'id' => array('name'=>'id','type'=>'xsd:int'),
                'siteName' => array('name'=>'siteName','type'=>'xsd:string'),
                'workPlace' => array('name'=>'workPlace','type'=>'xsd:string'),
                'situation' => array('name'=>'situation','type'=>'xsd:string')
        )
);
// *************************************************************************
// Complex Array ++++++++++++++++++++++++++++++++++++++++++
$server->wsdl->addComplexType(
        'userSiteDataList',
        'complexType',
        'array',
        '',
        'SOAP-ENC:Array',
        array(),
        array(
            array(
                'ref' => 'SOAP-ENC:arrayType',
                'wsdl:arrayType' => 'tns:userSitesData[]'
            )
        ),
        'tns:userSiteDataList'
);
//Struct of above array
$server->wsdl->addComplexType(
    'UserSiteList',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'list' => array('name'=>'list','type'=>'tns:userSiteDataList')
    )
);
// *************************************************************************
$server->register(
                "userSitesList",
                array('name' => 'xsd:int'),
                array('return' => 'tns:UserSiteList'),
                // namespace:
                $namespace,
                // soapaction: (use default)
                false,
                // style: rpc or document
                'rpc',
                // use: encoded or literal
                'encoded',
                // description: documentation for the method
                'Use this service to list buildingsites belong given user.');
// *************************************************************************
//RETURN USER'S BUILDING SITES LIST
function userSitesList($id)
{
    $list=array();
    $conn=openConnection();
    // Check connection
    if ($conn->connect_error)
    {
        $error=("Connection failed: " . $conn->connect_error);
    }   
    /* create a prepared statement */
    $stmt =  $conn->stmt_init();
    // prepare and bind
    if($stmt = $conn->prepare("SELECT id, siteName, workPlace, situation FROM sertkatestdatabase.buildingsite JOIN userworkssites ON  sertkatestdatabase.userworkssites.userId=? WHERE sertkatestdatabase.buildingsite.id=sertkatestdatabase.userworkssites.buildingsiteId"))
    {
        $stmt->bind_param("i", $id);
        // execute query
        $stmt->execute();
        // bind result variables //
        $stmt->bind_result($resultId,$resultSiteName,$resultWorkPlace,$resultSituation);
        // fetch value //       
        while ( $stmt->fetch() ) 
        {
            $list[]=array('id' => $resultId, 'siteName' => $resultSiteName, 'workPlace'=>$resultWorkPlace, 'situation'=>$resultSituation);
            //array_push($list,array('id' => 1, 'siteName' => 'test', 'workPlace'=>'test', 'situation'=>'test'));// I also try this. It doesn't work.
        }
        // close statement
        $stmt->close();
    }   
    $conn->close();
     // Create List
    $myList = array('list'=>$list);
    return $myList;
}

Solution

  • by looking at the code,

    The 3rd complex type "UserSiteList" is not required. Also you can append elements to the $myList array using,

    array_push($myList,$list); //inside while loop
    

    Please refer to the following sample code which returns the array of Employee structs,

    <?php
    
    require_once "lib/nusoap.php";
    
    function getEmployees($city){
    
        error_reporting(E_ALL & ~E_NOTICE);
    
        $arrayOfEmployees = array();
    
        $employeeArray1 = array();
        $employeeArray1['ID'] = 1;
        $employeeArray1['NAME'] = "Robert";
        $employeeArray1['City'] = "Newyork";
        array_push($arrayOfEmployees,$employeeArray1);
    
        $employeeArray2 = array();
        $employeeArray2['ID'] = 2;
        $employeeArray2['NAME'] = "Micheal";
        $employeeArray2['City'] = "Newyork";
        array_push($arrayOfEmployees,$employeeArray2);
    
        $employeeArray3 = array();
        $employeeArray3['ID'] = 3;
        $employeeArray3['NAME'] = "David";
        $employeeArray3['City'] = "Washington";
        array_push($arrayOfEmployees,$employeeArray3);
    
        $employeeArray4 = array();
        $employeeArray4['ID'] = 3;
        $employeeArray4['NAME'] = "Andy";
        $employeeArray4['City'] = "Newyork";
        array_push($arrayOfEmployees,$employeeArray4);
    
        //filter employees by city
        foreach($arrayOfEmployees as $elementKey => $element) {
        foreach($element as $valueKey => $value) {
            if($valueKey == 'City' && $value != $city){
                unset($arrayOfEmployees[$elementKey]);
            } 
        }
    }
    
    
        return $arrayOfEmployees;
    
    }
    
    $server = new soap_server();
    $namespace = 'http://localhost/test_soap_service/get_employee_list.php?WSDL';
    $server->configureWSDL('GetEmployeesService', $namespace);
    
    //create a complex type for Employee
    $server->wsdl->addComplexType('Employee','complexType','struct','all','',
    array(
    'ID' => array('name' => 'ID','type' => 'xsd:int'),
    'NAME' => array('name' => 'NAME','type' => 'xsd:string'),
    'Age' => array('name' => 'Age','type' => 'xsd:int')
        )
    );
    
    //create a complex type for Array of Employees
    $server->wsdl->addComplexType(
        'arrayOfEmployees',
        'complexType',
        'array',
        '',
        'SOAP-ENC:Array',
        array(),
        array(
            array(
                'ref' => 'SOAP-ENC:arrayType',
                'wsdl:arrayType' => 'tns:Employee[]'
            )
        )
    
    );
    
    
    $server->register("getEmployees",
        array('city' => 'xsd:string'),
        array('return' => 'tns:arrayOfEmployees')
        );
    
    if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
    $server->service($HTTP_RAW_POST_DATA);
    
    ?>