gpt4 book ai didi

php - 将数组传递给 web 服务 php nusoap

转载 作者:可可西里 更新时间:2023-10-31 23:47:48 26 4
gpt4 key购买 nike

我在将数组传递到我用 php 和 nusoap 创建的网络服务函数时遇到问题。问题是我想我做错了什么..我怀疑问题出在功能或我的客户端。当我发送一个数组时,它不会进入我在网络服务中注册的功能。当我调用网络服务时,我只得到空数组的响应。我希望有人能在这里帮助我。谢谢。

编辑:我设法修复了它。我忘了为请求构建结构。

服务器端:

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

)

最佳答案

好的,所以我修复了它。我添加了评论,以便任何人都可以看到我添加的内容以使其正常工作..

我需要为请求创建一个结构,以便我的函数知道数组类型。也在函数注册中修复了它。

关于php - 将数组传递给 web 服务 php nusoap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26502512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com