在 PHP Web 服务中返回对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:41:18  来源:igfitidea点击:

Returning An Array of Objects in PHP Web Service

phpweb-serviceswsdlnusoap

提问by gouwsmeister

I want to return an array of article objects in a PHP web service, using nuSOAP v 1.114. This is how I set up the WSDL:

我想使用 nuSOAP v 1.114 在 PHP Web 服务中返回一组文章对象。这是我设置 WSDL 的方式:

$server->wsdl->addComplexType(
'ArticleType',
'complexType',
'struct',
'all',
'',
array('articleId' => array('name'=>'articleId', 'type'=>'xsd:int'),
      'heading' => array('name'=>'heading', 'type'=>'xsd:string'),
      'text' => array('name'=>'text', 'type'=>'xsd:string')
     )
); 


$server->wsdl->addComplexType(
'ArrayOfArticleType',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
    array('ref' => 'SOAP-ENC:arrayType',
          'wsdl:arrayType' => 'tns:ArticleType[]'       // ArticleType[]
        )               
    ),
'tns:ArticleType'
);

My PHP Article class is very simple:

我的 PHP 文章类非常简单:

class Article {
public $articleId;
public $heading;
public $text;

public function __construct($articleId, $heading, $text=NULL) {
    $this->articleId = $articleId;
    $this->heading = $heading;
    $this->text = $text;
}
}

If I return just a new Article Object, like this:

如果我只返回一个新的文章对象,如下所示:

function TestArrayReturn() {
    $arr =  new Article(12345, "Test heading", "Test text.");
    //$arr2 = array($arr);  
    return $arr;
 }

the function, registered as:

函数,注册为:

$server->register("TestArrayReturn", array(), array('return'=>'tns:ArticleType'), $namespace, $namespace."#TestArrayReturn", 'rpc', 'encoded', 'Test function');

works fine, and returns the article as if it's an Array. However, if I try and return an ArrayOfArticleType (the commented line in TestArrayReturn() ), and register the function as return type tns:ArrayOfArticleType, then it fails with Error: HTTP Error: no data present after HTTP headers.

工作正常,并返回文章,就好像它是一个数组。但是,如果我尝试返回一个 ArrayOfArticleType(TestArrayReturn() 中的注释行),并将该函数注册为返回类型 tns:ArrayOfArticleType,那么它会失败并出现错误:HTTP 错误:HTTP 标头后不存在数据。

If, however, I create an ARRAY of ARRAYS manually, like so:

但是,如果我手动创建 ARRAY 的 ARRAY,如下所示:

$arr = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
$arr2 = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
return array($arr, $arr2);

it works!? What is wrong with my ArrayOfArticleType WSDL description that doesn't allow it to correctly serialize ArticleType objects, but correctly serialize an associative array with the same properties ("articleId", "heading", "text") ??

有用!?我的 ArrayOfArticleType WSDL 描述有什么问题,它不允许它正确序列化 ArticleType 对象,但正确序列化具有相同属性(“articleId”、“heading”、“text”)的关联数组?

Sorry for all the code, but I feel it's necessary to pinpoint the error I'm overlooking. Any help appreciated, I've been struggling with setting up the WSDL for this service for days.

抱歉所有代码,但我觉得有必要查明我忽略的错误。任何帮助表示赞赏,我一直在努力为此服务设置 WSDL 好几天。

采纳答案by gouwsmeister

I fixed this error. For anyone interested, this seems to be a bug in NuSOAP. You MUST register your returnType for the function as xsd:Array for it to correctly return the array, even though correct WSDL would constitute it as 'tns:ArrayOfArticleType'. I found this in some Drupal source code:

我修复了这个错误。对于任何感兴趣的人,这似乎是 NuSOAP 中的一个错误。您必须将函数的 returnType 注册为 xsd:Array 以使其正确返回数组,即使正确的 WSDL 会将其构成为“tns:ArrayOfArticleType”。我在一些 Drupal 源代码中发现了这个:

// Set return value for the service
$return = array();
if ($method['#return']) {
  **// Don't let a struct be declared as return parameter, because nusoap will not
  // Send back anything.**
  $return['return'] = 'xsd:'. $method['#return'];
  if ($method['#return'] == 'struct' || $method['#return'] == 'array') {
    $return['return'] = 'xsd:Array';
  }
} 

Hope this helps someone else who struggled with the same problem.

希望这可以帮助其他遇到同样问题的人。

回答by nikunj gandhi

Here is example both client and server for how to return an array with php web service.It is easy example to understand http://my-source-codes.blogspot.com/2011/10/php-web-service-return-array.html

这是客户端和服务器如何使用 php 网络服务返回数组的示例。这是一个很容易理解的示例 http://my-source-codes.blogspot.com/2011/10/php-web-service-return-数组.html