简体   繁体   English

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

[英]How to return nested array using Nusoap complextype in PHP?

I am writing a web service in php using nusoap library.我正在使用 nusoap 库在 php 中编写 Web 服务。 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;
}

by looking at the code,通过查看代码,

The 3rd complex type "UserSiteList" is not required.不需要第三个复杂类型“UserSiteList”。 Also you can append elements to the $myList array using,您也可以使用以下方法将元素附加到 $myList 数组,

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

Please refer to the following sample code which returns the array of Employee structs,请参考以下示例代码,它返回 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);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM