如何在 PHP 中使用 Nusoap complextype return 嵌套数组?

How to return nested array using Nusoap complextype in PHP?

我正在 php 使用 nusoap 库编写 Web 服务。我可以 return 简单变量,例如字符串、整数,也只有一种复杂类型,即结构。但是我不能 return 结构数组。我在一周内研究了几乎所有关于此的示例,但它不起作用。那么我哪里错了?

代码

$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;
}

通过查看代码,

第 3 个复杂类型 "UserSiteList" 不是必需的。 您还可以使用

将元素附加到 $myList 数组
array_push($myList,$list); //inside while loop

请参考下面的示例代码,其中returns Employee 结构数组,

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

?>