问题
I'm writing a PHP client to a C#/WCF web service. The parameters for some of the calls consist of instances of business classes that I instantiate in PHP and pass via HTTP POSTs in SOAP formatted payloads to the webservice. The constructors for some of these classes have one or more optional parameters, of which some are of type Int32.
When I set these parameters as optional in PHP and then send the instances up to the webservice they are being transmitted as empty strings (well, more specifically as empty nodes: <ns1:order />
in the example below). This happens in either of the following models:
class ThingOne {
/**
* @var string $name
*/
/**
* @var int $order
*/
__construct($name, $order = null) {
$this->name = $name;
$this->order = $order;
}
}
or
class ThingOne {
/**
* @var string $name
*/
/**
* @var int $order
*/
__construct($name, $order = null) {
$this->name = $name;
if(isset($order) {
$this->order = $order;
}
}
}
Once that object gets to the webservice, C# can't deserilize it, saying The value '' cannot be parsed as the type 'Int32'
.
If I default the value to 0 then everything works fine (as I would expect, given the error message). The problem is that the webservice has business logic to default those values if they're not provided, and I don't want to reproduce that logic in my client.
Is there any way to instruct PHP's SoapClient not to send the null properties at all if they're not set, instead of sending an empty node?
回答1:
After closer analysis I determined that this problem was in the WSDL definition for the service - some business-logic-optional properties were not marked as nillable in the WSDL, and in that case the SoapClient (perfectly correctly) passes an empty node.
When the WSDL properly defines the property as nillable, when that property is set to null the node is marked nil="true"
and doesn't show up as an empty string on the endpoint.
来源:https://stackoverflow.com/questions/9213538/null-values-in-php-soapclient-calls