简体   繁体   中英

passing an array to web-service php nusoap

i have a problem with passing an array to a web-service function that i created with php and nusoap. the problem is that i guess i'm doing something wrong.. i suspect that the problem is in the function or in my client side. when i send an array it doesn't go into the function i registered with the web-service. i only get response of empty array when i call the web-service. i hope someone can help me here. thanks.

EDIT: I managed to fix it. i forgot to build a struct for the request.

server side:

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

?>

client side

<?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>";
    }
}

?>

this is the output(respond)

Respond

Array
(
    [0] => Array
        (
            [name] => 
            [code] => 
            [price] => 
            [quantity] => 
            [total_price] => 0
        )

)

EDIT:
this is the fixed output.

Respond

Array
(
    [0] => Array
        (
            [name] => GoPro
            [code] => 245
            [price] => 70
            [quantity] => 4
            [total_price] => 280
        )

)

ok so i fixed it. i added comments so anyone can see what i added in order to make it work..

i needed to make a struct for the request so my function will know the array type. also fixed it in the function registration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM