使用带复杂类型的nusoap调用服务器

时间:2014-03-27 06:29:18

标签: php web-services wsdl nusoap

我在浏览器中调用客户端,并将一个数组数组作为参数发送到服务器函数,它返回第一个键值对,但它只发送第二个数组的键而不是它的值,请帮帮我。

客户端

<?php 

    require_once("nuSOAP/lib/nusoap.php");


    $id = array('ID'=>'1234','type' => 'int');

    $MyComplexType = array(
                        'ID'=> $id,
                        'YourName' =>array('YourName' => '123','type' => 'string')
                        );


    //Create object that referer a web services
    $client = new soapclient('http://localhost/server/server2.php');
    //Call a function at server and send parameters too
    $response = $client->call('HelloComplexWorld',$MyComplexType);
    //Process result
    if($client->fault)
    {
      echo "FAULT: <p>Code: (".$client->faultcode."</p>";
      echo "String: ".$client->faultstring;
    }
    else
    {
            echo"<pre>";
            print_r($response);
            echo "</pre>";
            exit();
    }

 ?>

服务器

<?php 

    require_once("nuSOAP/lib/nusoap.php");
    $namespace = "http://localhost/server/server2.php";

    // create a new soap server
    $server = new soap_server();

    // configure our WSDL
    $server->configureWSDL("HelloExample");

    // set our namespace
    $server->wsdl->schemaTargetNamespace = $namespace;

    //Register a method that has parameters and return types
    $server->register(
    // method name:
    'HelloWorld',
    // parameter list:
    array('name'=>'xsd:string'),
    // return value(s):
    array('return'=>'xsd:string'),
    // namespace:
    $namespace,
    // soapaction: (use default)
    false,
    // style: rpc or document
    'rpc',
    // use: encoded or literal
    'encoded',
    // description: documentation for the method
    'Simple Hello World Method');

    //Create a complex type
    $server->wsdl->addComplexType(
        'MyComplexType',
        'complexType',
        'struct',
        'all',
        '',
    array( 
        'ID' => array(
            'name' => 'ID',
            'type' => 'xsd:int'
            ),
        'YourName' => array(
            'name' => 'YourName',
            'type' => 'xsd:string'
            )

        )
    );

    //Register our method using the complex type
    $server->register(
    // method name:
    'HelloComplexWorld',
    // parameter list:
    array('name'=>'tns:MyComplexType'),
    // return value(s):
    array('return'=>'tns:MyComplexType'),
    // namespace:
    $namespace,
    // soapaction: (use default)
    false,
    // style: rpc or document
    'rpc',
    // use: encoded or literal
    'encoded',
    // description: documentation for the method
    'Complex Hello World Method');

    //Our Simple method
    function HelloWorld($name)
    {
    return "Hello " . $name;
    }

    //Our complex method
    function HelloComplexWorld($mycomplextype)
    {

    return $mycomplextype;
    }

    // Get our posted data if the service is being consumed
    // otherwise leave this data blank.
    $POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

    // pass our posted data (or nothing) to the soap service
    $server->service($POST_DATA);
    exit();


 ?>

输出

Array
(
    [ID] => 1234
    [YourName] => 
)

1 个答案:

答案 0 :(得分:0)

使用以下参数

正确使用
    $MyComplexType = array('ID' => $id ,'YourName' => "Jon Postel");

这给出了

Array
  (
    [ID] => 1234
    [YourName] => "Jon Postel"
 )