6

I would like to return an array of string in my web services

I've tryed :

<?php
require_once('nusoap/nusoap.php');

$server = new soap_server();
$server->configureWSDL('NewsService', 'urn:NewsService');
$server->register('GetAllNews', 
 array(),
 array('return' => 'xsd:string[]'),
 'urn:NewsService',
 'urn:NewsService#GetAllNews',
 'rpc',
 'literal',
 ''
);

// Define the method as a PHP function
function GetAllNews()
{
 $stack = array("orange", "banana");
 array_push($stack, "apple", "raspberry");
 return $stack;
}

but it doesn't work. What is the correct syntax for that ?

Thanks in advance for any help

3 Answers 3

9

You first need to define a new type that describes an array of strings like so:

$server->wsdl->addComplexType(
  'ArrayOfString',
  'complexType',
  'array',
  'sequence',
  '',
  array(
    'itemName' => array(
      'name' => 'itemName', 
      'type' => 'xsd:string',
      'minOccurs' => '0', 
      'maxOccurs' => 'unbounded'
    )
  )
);

Then you can use tns:ArrayOfString as the return type.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I convert the data type of $stack (a simple array of string in php) to ArrayOfString so that I can get the data of $stack as ArrayOfString in my client code (C#) ?
@Ankit: Sorry, can't help you with that. I wrote this a long time ago.
@Computer User you return the value directly and it will be casted automaticly
1

This site describes a nice way to return complex datatypes and receive it with C#: http://sanity-free.org/125/php_webservices_and_csharp_dotnet_soap_clients.html

1 Comment

This site's code works with WCF. Don't use SEQUENCE (leave the composition parameter to AddComplexType blank), but instead use the restriction type "SOAP-ENC:Array".
1

When returning array of arrays, you might need a different configuration from Oliver. For example phfunc2php uses this technique in the nusoapcode.class.php file (https://github.com/sylnsr/pgfunc2php/blob/master/nusoapcode.class.php). The code it generates looks like so:

$server->wsdl->addComplexType(
    'ArrayOfArrays','complexType','array','',
    'SOAP-ENC:Array',
    array(),
    array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]')));

and then the functions simply needs to return "tnsArrayOfArrays:

$server->register(
    'sel_signon_id_by_uuid',
    array('user_uuid' => 'xsd:string'),
    array('return'=>'tns:ArrayOfArrays'),

The project mentioned above can compile working code for you, should you want to see this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.