【问题标题】:WCF service with PHP client - complex type as parameter not working带有 PHP 客户端的 WCF 服务 - 复杂类型作为参数不起作用
【发布时间】:2010-10-04 01:29:03
【问题描述】:

我有一个包含三种方法的 WCF 服务。其中两个方法返回自定义类型(这些按预期工作),第三个方法将自定义类型作为参数并返回一个布尔值。当通过 PHP soap 客户端调用第三个方法时,它返回一个“对象引用未设置为对象的实例”异常。

示例自定义类型:

_ 公开课 MyClass

Private _propertyA As Double
<DataMember()> _
Public Property PropertyA() As Double
    Get
        Return _propertyA
    End Get
    Set(ByVal value As Double)
        _propertyA = value
    End Set
End Property

Private _propertyB As Double
<DataMember()> _
Public Property PropertyB() As Double
    Get
        Return _propertyB
    End Get
    Set(ByVal value As Double)
        _propertyB = value
    End Set
End Property

Private _propertyC As Date
<DataMember()> _
Public Property PropertyC() As Date
    Get
        Return _propertyC
    End Get
    Set(ByVal value As Date)
        _propertyC = value
    End Set
End Property

结束类

方法:

公共函数 Add(ByVal param As MyClass) As Boolean 实现 IService1.Add ' ... 结束函数

PHP 客户端调用:

$client->Add(array('param'=>array( '属性A' => 1, '属性B' => 2, 'PropertyC' => "2009-01-01" )));

WCF 服务在 .Net 客户端上运行良好,但我是 PHP 新手,无法使其正常工作。

是否可以在 PHP 中创建“MyClass”的实例。

任何帮助将不胜感激。

注意:我使用的是 PHP 5(XAMPP 1.7.0 for Windows)。

谢谢

马特

【问题讨论】:

  • 你试过 $myClassInstance = new $Client->MyClass();
  • Matt -- 你有没有找到解决这个问题的方法?我遇到了类似的问题,PHP 无法将类实例传递给 Web 服务。
  • @Steve:请看下面我的回答。
  • 我们最终解决了我们的问题。它是 PHP 中 array() 的嵌套。我们需要另一个 array() 嵌套来使其工作。 .NET 似乎在 SOAP 中添加了另一个抽象层,仅查看 .NET 中的服务定义并不清楚。

标签: .net php wcf


【解决方案1】:

我愿意打赌这是因为您使用 Date 类型作为参数之一,并且它没有通过 PHP 正确序列化。尝试使用字符串并手动解析它。我知道,不是很安全,但你能做什么?

【讨论】:

    【解决方案2】:

    我已尝试将 MyClass 中的所有属性类型更改为 String,但仍然出现相同的错误。

    我还尝试使用 $myClassInstance = new $Client->MyClass(); 来实例化 MyClass;但这会产生以下错误:

    "类名必须是合法的对象或filename.php中的字符串"

    上面是否需要服务命名空间,例如$myClassInstance = new $Client->Namespace\MyClass()?

    服务的 wsdl 不包含 MyClass 的任何描述。 MyClass 类具有 DataContract()ServiceKnownType(GetType(MyClass)) 属性,并且所有属性都应用了 DataMember() 属性,但wsdl 中仍然没有提到 MyClass。这可能是php无法实例化它的原因吗?

    谢谢

    马特

    【讨论】:

      【解决方案3】:

      此代码

      $myClassInstance = new $Client->MyClass();是语法错误

      但是这段代码

      $myClassInstance = 新的 $Client->MyClass;虽然这段代码有效

      这是正确的语法 去掉双括号

      【讨论】:

        【解决方案4】:

        wcf 中的 wsdl 与旧的 Web 服务相比有很大不同,它将 xsd 拆分为不同的文件。请仔细阅读你的wsdl,你应该可以看到这样的几行

        这是你的xsd。

        【讨论】:

          【解决方案5】:

          我不再设置 XAMPP 来进行测试,但这里有一些示例代码:

          PHP:

          $wsdl = "https://....../ServiceName.svc?wsdl";
          $endpoint = "https://...../ServiceName.svc/endpointName";
          $client = new SoapClient($wsdl, array('location'=>$endpoint));
          
          $container = new stdClass();
          
          $container->request->PropertyA = 'Test 1';
          $container->request->PropertyB = 'Test 2';
          $container->request->PropertyC = '05/10/2010';
          
          $response = $client->ServiceMethodA($container);
          

          request 是 Web 服务预期的参数名称。

          如果您有一个引用其他自定义类型的自定义类型,您可以按如下方式设置这些属性:

          $container->request->OtherCustomType->Property1 = 'Test';
          

          希望对您有所帮助。

          【讨论】:

            猜你喜欢
            • 2012-01-12
            • 1970-01-01
            • 2010-11-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多