PHP SoapClient 调用中的空值 [英] Null values in PHP SoapClient calls

查看:91
本文介绍了PHP SoapClient 调用中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 C#/WCF Web 服务编写 PHP 客户端.一些调用的参数包含我在 PHP 中实例化的业务类的实例,并通过 SOAP 格式的有效负载中的 HTTP POST 传递到 Web 服务.其中一些类的构造函数有一个或多个可选参数,其中一些是 Int32 类型.

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.

当我在 PHP 中将这些参数设置为可选参数,然后将实例发送到网络服务时,它们将作为空字符串传输(更具体地说,作为空节点:<ns1:order/> 在下面的示例中).这发生在以下任一模型中:

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

class ThingOne {
    /**
     * @var string $name
     */

    /**
     * @var int $order
     */

    __construct($name, $order = null) {
        $this->name = $name;
        if(isset($order) {
            $this->order = $order;
        }
    }
}

一旦该对象到达网络服务,C# 就无法对其进行反序列化,说值 '' 无法解析为类型 'Int32'.

Once that object gets to the webservice, C# can't deserilize it, saying The value '' cannot be parsed as the type 'Int32'.

如果我将值默认为 0,则一切正常(鉴于错误消息,正如我所料).问题在于,如果未提供这些值,Web 服务具有默认这些值的业务逻辑,而我不想在我的客户端中重现该逻辑.

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.

有什么方法可以指示 PHP 的 SoapClient 如果未设置 null 属性,则根本不发送它们,而不是发送一个空节点?

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?

推荐答案

经过仔细分析,我确定这个问题出在服务的 WSDL 定义中——一些业务逻辑可选属性在 WSDL 中没有标记为可空,在这种情况下,SoapClient(完全正确)传递一个空节点.

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.

当 WSDL 将属性正确定义为 nillable 时,当该属性设置为 null 时,节点被标记为 nil="true" 并且不会在端点上显示为空字符串.

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.

这篇关于PHP SoapClient 调用中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆