Consuming a WCF Service from PHP
Below is an example PHP script which accesses the sample WCF Service defined by the WCF template in Visual Studio 2010.
-
GetDataUsingDataContract(CompositeType composite)
This function is declared by the template WCF Service in Visual Studio 2010. It accepts a ‘CompositeType’ object as the parameter, which is itself defined within the webservice. This object has two properties, a bool called ‘boolValue’, and a string called ‘stringValue’.
To pass a parameter from PHP, a similar class has to be defined within the PHP as well.
//Declare the class that will be passed as the parameter
-
class myCompositeType{
-
public $boolValue;
-
public $stringValue;
-
}
-
-
//instantiate an object of the class we just created
-
$obj = new myCompositeType();
-
//Assign values to the properties of the class
-
$obj->boolValue=true;
-
$obj->stringValue="World!";
-
-
//Wrap up the object before it can be passed
-
//Ensure that the array key is exactly the same as the expected parameter name declared in the webservice.
-
$myParameter = array('composite'=>$obj);
-
-
//Create a SOAP client
-
$client = new SoapClient("path to your .wsdl");
-
-
//Call the method, passing our parameter
-
$retVal = $client->GetDataUsingDataContract($myParameter);
-
-
//Handle the result, which is also an object of the myCompositeType class
-
echo $retVal->GetDataUsingDataContractResult->stringVal;
(Ensure that the array key is exactly the same as the expected parameter name declared in the webservice.)
