在PHP Web服务中返回对象数组 [英] Returning An Array of Objects in PHP Web Service

查看:136
本文介绍了在PHP Web服务中返回对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

我的PHP Article类非常简单:

My PHP Article class is very simple:

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

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

如果我只返回一个新的Article对象,像这样:

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

该功能,注册为:

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

可以正常工作,并返回该文章,就好像它是一个数组一样.但是,如果我尝试返回ArrayOfArticleType(TestArrayReturn()中的注释行),并将该函数注册为返回类型tns:ArrayOfArticleType,则它将失败,并显示错误:HTTP错误:HTTP标头后没有数据.

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.

但是,如果我手动创建一个阵列的阵列,就像这样:

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

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

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

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

对不起所有代码,但是我觉得有必要查明我忽略的错误.感谢您的任何帮助,几天来我一直在努力为此服务设置WSDL.

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.

推荐答案

我修复了此错误.对于任何有兴趣的人来说,这似乎是NuSOAP中的错误.您必须将函数的returnType注册为xsd:Array才能正确返回该数组,即使正确的WSDL会将其构成为'tns:ArrayOfArticleType'.我在一些Drupal源代码中发现了这一点:

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:

// 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.

这篇关于在PHP Web服务中返回对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆