php - 将多维数组传递给 PHP 中的 soap complexType

标签 php soap multidimensional-array complextype

我正在从我的邮件服务器中提取一些电子邮件。有一个函数应该提取这些电子邮件并返回一个多维数组。我在客户端 Web 服务器中使用此数组来为我完成这项工作。我不知道如何将这个数组传递给 soap complexType。我写了下面的代码:

$server->wsdl->addComplexType(
'MailTicket',
'complexType',
'struct',
'all',
'',
 array(
    'attachment' => array('name' => 'attachment', 'type' => 'xsd:string'),
    'body' => array('name' => 'body', 'type' => 'xsd:string'),
    'accountID' => array('name' => 'accountID', 'type' => 'xsd:string')
 )
);

$server->wsdl->addComplexType(
 'MailTicketReturn',
 'complexType',
 'struct',
 'all',
 '',
 array(
    'Done' => array('name' => 'result', 'type' => 'xsd:string')
 )
);

   // Register the method to expose
   $server->register('createMailTicket',                    // method name
 array('mailTicketData' => 'tns:MailTicket'),          // input parameters
 array('return' => 'tns:MailTicketReturn'),    // output parameters
 'urn:eticketing',                         // namespace
 'urn:eticketing#createMailTicket',                   // soapaction
 'rpc',                                    // style
 'encoded',                                // use
 'create a ticket by mail'        // documentation
);

在客户端,我写道:

require_once('nusoap.php');
$wsdlURL="http://127.0.0.1/eticket/ETKWS.php?wsdl";
$client = new nusoap_client($wsdlURL,true);
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;


$finalArray=Array
(
  [attachment] => Array
    (
        [0] => Array
            (
                [0] => file1
                [1] => file2
            )

        [1] => Array
            (
                [0] => file1x
            )

    )
[body]=>Array
            (
                [0] => some text
                [1] => some other text
            )

[accountID] => Array
    (
        [0] => 5464654
        [1] => 4654664
    )

)

if(is_array($finalArray)) // creat new tickets
{
 $result=$client->call('createMailTicket',$finalArray);
}

$err = $client->getError();
if ($err) {
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) .       '</pre>';
exit();
}

我得到了这个错误:

构造函数错误

在第 1 行解析 SOAP 负载时出现 XML 错误:格式不正确( token 无效)

最佳答案

NuSOAP 支持返回多维数组(xsd:Array)

           $server= new nusoap_server();

            $namespace = "http://localhost/webservice/";
           // create a new soap server
           $server = new nusoap_server();
           // configure our WSDL
           $server->configureWSDL("WebServices212");
           // set our namespace
           $server->wsdl->schemaTargetNamespace = $namespace;          

           $server->register(
            // method name:
            'test',          
            // parameter list:
            array('id'=>'xsd:int'), 
            // return value(array()):
            array('return'=>'xsd:Array'),
            // namespace:
            $namespace,
            // soapaction: (use default)
            false,
            // style: rpc or document
            'rpc',
            // use: encoded or literal
            'encoded',
            // description: documentation for the method
            'documentation');

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

客户端

           client=new nusoap_client("http://localhost/webservice /webservices.php?wsdl");
          $client->setCredentials("webadmin","****");

           $err = $client->getError();
          if ($err) {

               echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';

            }

            $result = $client->call('test', array('id' => '1'));
            print_r($result);

如果你使用 PHP 的 web 服务没问题,但在其他语言中存在兼容性问题

关于php - 将多维数组传递给 PHP 中的 soap complexType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483136/

相关文章:

使用 SOAP 的 IOS Swift 调用 Web 服务

java - 查找二维数组中数字的位置

php - 如何使用数组函数合并 php 中的内部数组元素?

javascript - 模态窗口在提交时关闭

Php:如果输入值为 x、y 或 z,则执行操作

android - 哪个是在 Android 中处理 SOAP 响应的最佳方式

java - ConnectFour Java 游戏问题

php - ajax长轮询url/controller/method后面的数字?_xxxx

php - 反复尝试使用 PHP 执行 MySQL 查询

.net - 未定义的complexType 'http://schmeas.xmlsoap.org/soap/encoding/:Array'用作复杂类型限制的基础