Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF service with PHP client - complex type as parameter not working

Tags:

.net

php

wcf

I have a WCF service with three methods. Two of the methods return custom types (these work as expected), and the third method takes a custom type as a parameter and returns a boolean. When calling the third method via a PHP soap client it returns an 'Object reference not set to an instance of an object' exception.

Example Custom Type:

_ Public Class 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

End Class

Method:

Public Function Add(ByVal param As MyClass) As Boolean Implements IService1.Add ' ... End Function

PHP client call:

$client->Add(array('param'=>array( 'PropertyA' => 1, 'PropertyB' => 2, 'PropertyC' => "2009-01-01" )));

The WCF service works fine with a .Net client but I'm new to PHP and can't get this to work.

Is it possible to create an instance of 'MyClass' in PHP.

Any help would be appreciated.

Note: I'm using PHP 5 (XAMPP 1.7.0 for Windows).

Thanks

Matt

like image 438
Matt F Avatar asked Mar 09 '26 05:03

Matt F


2 Answers

I no longer have XAMPP setup in order to test but here is some example code:

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 is the name of the parameter expected by the web service.

If you have a custom type with references to other custom types you can set those properties as follows:

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

Hope that helps.

like image 141
Matt F Avatar answered Mar 10 '26 20:03

Matt F


I'd be willing to bet it's because you're using a Date type as one of your parameters and it's not serializing properly through PHP. Try using a string and parsing it manually. I know, not very type safe, but what can you do?

like image 38
Randolpho Avatar answered Mar 10 '26 20:03

Randolpho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!