Web Services 将数组传递给Web服务php nusoap

sigwle7e  于 2022-11-15  发布在  PHP
关注(0)|答案(1)|浏览(175)

我有一个问题,传递一个数组到一个web服务函数,我创建了php和nusoap。问题是,我猜我做错了什么。我怀疑问题是在函数或在我的客户端。当我发送一个数组,它没有进入我注册的web服务的函数。我只得到空数组的响应,当我调用web服务。我希望有人能帮我谢谢

**EDIT:**我设法修复了它。我忘记为请求构建一个结构。
服务器端:

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

)
kkih6yb8

kkih6yb81#

好的,所以我修复了它。我添加了评论,这样任何人都可以看到我添加了什么,以便使它工作。
我需要为请求创建一个结构,这样我的函数就可以知道数组类型。我还在函数注册中修复了它。

相关问题