1

I have a SOAP server with a method that returns a boolean indicating success and a string indicating an error message (empty on success). The WDSL file defines this as follows:

<message name="configureDisplayerRequest">
    <part name="id" type="xsd:string" />
    <part name="power" type="xsd:boolean" />
    <part name="color" type="xsd:string" />
    <part name="text" type="xsd:string" />
</message>

<message name="configureDisplayerResponse">
    <part name="result" type="xsd:boolean" />
    <part name="errorMessage" type="xsd:string" />
</message>

How do I return multiple values from my PHP code?

On my client side, written in C# using a client generated by Visual Studio, the method has the following signature:

public bool configureDisplayer(string id, bool power, string color, string text, out string errorMessage)

I tried to do something similar on the PHP server-side assuming that it might work with a bit of luck, but it doesn't:

public function configureDisplayer( $id, $power, $color, $text, & $errorMessage ) // Output parameter & $errorMessage, also returns a boolean.

If I change my WSDL so that the method only returns a single string, and then if I only return a single string in my PHP server side code, it works fine. However, how can I return more than a single value?

braaterAfrikaaner
  • 1,072
  • 10
  • 20
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • Can you return an array instead of a string? Then just split the array up again? –  Feb 05 '18 at 15:34
  • Why do you have a `&` before `$errorMessage`? Do you understand what the purpose of using references in PHP? – Exit Feb 05 '18 at 15:35
  • @Exit I beleive I do. Similar to the "out" of the C# function. Correct me if i'm wrong. – Virus721 Feb 05 '18 at 15:36
  • Also, it would be helpful to include the full PHP code for `configureDisplayer`. We can't guess what is happening inside. – Exit Feb 05 '18 at 15:36
  • @Exit Pretty much nothing. Right now i'm just trying to return hard coded values. – Virus721 Feb 05 '18 at 15:37
  • Here is some vital reading on references, using `&`: http://php.net/manual/en/language.references.php – Exit Feb 05 '18 at 15:40
  • @Exit I know how to use references, thank you... If you don't want or can't answer my question, please do not post more comments. My question isn't about how references work, it is about how to return more than a single value from a method of my PHP SOAP server. – Virus721 Feb 05 '18 at 15:42
  • Ultimately, your function can only return one item, but using references (or global variables), you can affect a variable that is outside of the scope of the function from within the function. `$a = 1; function inc(&$b) {$b++; return true;} $c = inc($a); echo $a; var_dump($c);` would increment the variable $a in public scope and return a boolean true. – Exit Feb 05 '18 at 15:48
  • @Virus721 Keep in mind, I have no way of knowing what you know, and can't help you without asking questions to make sure you understand certain things. Again, your code examples are too bare to provide an proper answer. Are you using methods within classes or functions? – Exit Feb 05 '18 at 15:51
  • @Exit Since PHP only allows a single return value, I assumed that, just like in C#, the caller of my method, i.e the SoapServer class, would pass the extra return values by reference after the parameters. That's not what happens and the PHP documentation doesn't provide information about how to return more than a single value. I'm using an instance method in a class called "TestWebService" which I'm providing to the SoapServer class using `$soapServer->setClass( "TestWebService" );`. – Virus721 Feb 05 '18 at 15:55
  • PHP can return an object as well, which can contain as many arguments as you like. Also, I'm glad to see that you are using `SoapServer`. You could always return a custom object or an even an array as Pascal mentioned. I assume you using the built in `SoapServer`? – Exit Feb 05 '18 at 16:02
  • Also, to be clear, when you are saying "return", are you meaning returning from a method or are you talking about outputting to the SOAP client? – Exit Feb 05 '18 at 16:04
  • @Exit Yes there are multiple ways I could use to return composite data, like encoding them into a single JSON string for example. But since the protocol allows me to return multiple values, I want to do it this way, first to know how to do, and second because its cleaner and simpler, IMO. By return I mean return a message to the client containing multiple "parts" as they call that. I tried returning an array of SoapParam instances, but it doesn't work either. EDIT : And yes i'm using the built-in SoapServer PHP module. – Virus721 Feb 05 '18 at 16:10
  • Did you figure this out? Again, it would be helpful to see more of your code. If I'm understanding correctly, your issue isn't with the method returning only one value, but that the resulting XML is not what you are expecting. If so, take a look at this which highlights a bug in SoapVar if a declaration is present: https://stackoverflow.com/questions/12736697/how-return-custom-xml-response-in-soapserver-response – Exit Feb 06 '18 at 02:14
  • @Exit I ended up using a single return value and hate SOAP as well as PHP. – Virus721 Feb 06 '18 at 16:41
  • SOAP can be annoying with the WDSL stuff, but there is nothing stopping you from just composing your own XML response to fit your needs. I may have done that in the past. – Exit Feb 06 '18 at 19:49

0 Answers0