我有一个问题,将数组传递给我用php和nusoap创建的Web服务函数。 问题是,我想我做错了什么..我怀疑问题出在功能或客户端。当我发送一个数组时,它不会进入我在网络服务中注册的功能。当我调用web服务时,我只得到空数组的响应。 我希望有人可以帮助我。 感谢。
编辑:我设法解决了这个问题。我忘了为请求构建一个结构。
服务器端:
<?php
//include nusoap
require_once('c:\\wamp\\www\\nusoap.php');
//create server instance
$server = new soap_server();
//configure wsdl
$server->wsdl->schemaTargetNamespaces = 'urn:GetArr';
$server->configureWSDL('GetArr','urn:GetArr');
$server->wsdl->addComplexType(
'Product',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'code' => array('name' => 'code', 'type' => 'xsd:string'),
'price' => array ('name' => 'price', 'type' => 'xsd:int'),
'quantity' => array ('name' => 'quantity', 'type' => 'xsd:int'),
'total_price' => array('name' => 'total_price', 'type' => 'xsd:int')
));
//this is what i was missing
$server->wsdl->addComplexType(
'ArrayReq',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'code' => array('name' => 'code', 'type' => 'xsd:string'),
'price' => array ('name' => 'price', 'type' => 'xsd:int'),
'quantity' => array ('name' => 'quantity', 'type' => 'xsd:int')
));
//until here.
$server->wsdl->addComplexType(
'ProductArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Product[]')),
'tns:Product');
//function that get and return values
function GetTotalPrice ($proArray) {
$temparray = array();
$temparray[] = array('name' => $proArray['name'], 'code' => $proArray['code'], 'price' => $proArray['price'], 'quantity'
=> $proArray['quantity'], 'total_price' => $proArray['quantity'] * $proArray['price']);
return $temparray;
};
//register the method
$server->register('GetTotalPrice',
array('proArray' => 'tns:ArrayReq'),// and this line also.
array('return' => 'tns:ProductArray'),
'urn:GetArr',
'urn:GetArr#GetTotalPrice',
'rpc',
'encoded',
'Get the product total price'
);
//run the service
$post = file_get_contents('php://input');
$server->service($post);
?>
客户端
<?php
//include nusoap
require_once('c:\\wamp\\www\\nusoap.php');
ini_set ('soap.wsdl_cache_enabled', 0);
$arr['name'] = "GoPro";
$arr['code'] = "245";
$arr['price'] =70;
$arr['quantity'] = 4;
$sClient = new nusoap_client('http://localhost/nusoap/comserv.php?wsdl','wsdl','','','','');
$response = $sClient->call('GetTotalPrice',array($arr),'','', false,true);
$error = $sClient->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
if ($sClient->fault) {
echo "<h2>Fault</h2><pre>";
echo ($response);
echo "</pre>";
}
else {
$error = $sClient->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
if ($response != "" || NULL){
echo "<h2>Respond</h2><pre>";
print_r ($response);
echo "</pre>";
}
}
?>
这是输出(响应)
Respond
Array
(
[0] => Array
(
[name] =>
[code] =>
[price] =>
[quantity] =>
[total_price] => 0
)
)
修改
这是固定输出。
回应
Array
(
[0] => Array
(
[name] => GoPro
[code] => 245
[price] => 70
[quantity] => 4
[total_price] => 280
)
)
答案 0 :(得分:0)
我需要为请求创建一个结构,所以我的函数将知道数组类型。还修改了功能注册。