[英]passing an array to web-service php nusoap
我在將數組傳遞給我使用 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
)
)
好的,所以我修好了。 我添加了評論,所以任何人都可以看到我添加的內容以使其工作..
我需要為請求創建一個結構,以便我的函數知道數組類型。 也在功能注冊中修復了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.