将数组传递给 web 服务 php nusoap

问题描述 投票:0回答:1

我在将数组传递给我用 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 arrays web-services wsdl nusoap
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.