3
votes

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

5
had you tried $myClassInstance = new $Client->MyClass(); - Oscar Cabrero
Matt -- did you ever find a solution to this? I'm having a similar problem with a PHP not being able to pass a class instance into a web service. - Steve Wranovsky
@Steve: See my answer below. - Matt F
we ended up solving our problem. It was the nesting of the array() in the PHP. We needed another array() nesting to make it work. It appears that .NET adds another abstraction layer in SOAP that isn't clear when just looking at the service definitions in .NET. - Steve Wranovsky

5 Answers

3
votes

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.

1
votes

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?

1
votes

I have tried changing all the property types in MyClass to String but still get the same error.

I also tried to instantiate MyClass using $myClassInstance = new $Client->MyClass(); but this generates the following error:

"Class name must be a valid object or a string in filename.php"

Is the service namespace needed above e.g. $myClassInstance = new $Client->Namespace\MyClass()?

The wsdl for the service does not contain any description of MyClass. The class MyClass has the DataContract() and ServiceKnownType(GetType(MyClass)) attribute and the properties all have the DataMember() attribute applied but still no mention of MyClass in the wsdl. Could this be the reason php can't instantiate it?

Thanks

Matt

1
votes

This Code

$myClassInstance = new $Client->MyClass(); is a syntax error

but this code

$myClassInstance = new $Client->MyClass; while this code works

this is the correct syntax remove the double parenthesis

1
votes

The wsdl in wcf is a huge different compare to old web service, it split xsd into different files. Please read carefully of you wsdl, you should be able to see several line like this

here is you xsd.