tags:

views:

1085

answers:

5

Hi,

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

+1  A: 

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?

Randolpho
A: 

Hi,

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

Matt F
A: 

Hi

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

Paul Evangel Tapon
A: 

Hi Matt

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.

Vincent
+1  A: 

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.

Matt F