我正在使用nusoap库在php中编写一个Web服务。我可以返回简单的变量,如字符串,整数,也只返回一个复杂类型,即struct。但我无法返回结构数组。我在一周内研究了几乎所有关于此的例子,但它不起作用。那么我哪里出错?
代码
$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;
}
答案 0 :(得分:0)
通过查看代码,
不需要第三种复杂类型“ UserSiteList”。 您也可以使用以下方式将元素附加到$ myList数组中:
array_push($myList,$list); //inside while loop
请参考以下示例代码,该示例代码返回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);
?>