Returning An Array of Objects in PHP Web Service

给你一囗甜甜゛ 提交于 2019-11-30 04:15:16

问题


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

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

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.

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

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

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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1459955/returning-an-array-of-objects-in-php-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!