Home » Php » Returning An Array of Objects in PHP Web Service

Returning An Array of Objects in PHP Web Service

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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