Accessing IIS Hosted WCF Services from PHP

Using PHP to access IIS hosted WCF services. Discussion and example.

                                      Accessing IIS Hosted WCF services from PHP

There have been many articles, published recently, on building and configuring IIS hosted “WCF services”. The number of performance and security enhancements in WCF services makes them very attractive for development community.  One of the main features of WCF is that they are meant to be “consumed” from any client (“Cross-platform Data Exchange”).  in our opinion, however,  there is an informational “white spot” on that very subject. The majority of available publications on the web discussing WCF service consumption use the “.Net platform”.   This is fine for developers whose business requirements allow them to stay strictly within the .Net environment. 

A recent client of ours had the need to access our web service using PHP.  They were given “.wsdl file” containing all the necessary information.  However, a day later, we were notified by the client that they could not access our service using their technology – PHP.  We therefore decided to try to connect to the WCF service using the client’s technology – PHP. This was somewhat of a challenge for us as we are not PHP experts -- as we specialize in Microsoft technologies.   

We invested some time researching basic PHP features on and after some trial and error, built a properly working PHP file. 

As we’ve done in recent articles, we share the results of our work with the “ community” in the hope that it will save some time and pain to other developers with similar needs.  In particular, we find that simple examples often clearly illustrate a technique often better than in textbooks – where one often finds it difficult to discern the essence of a technique.

Therefore, here is a simple working solution to access IIS hosted WCF service returning an object.

 $client = new SoapClient("your.wsdl");  //create a “SoapClient” using a given .wsdl file

 //declare an object which will be passed to WCF service:
class myClient
 public $FirstName = '';  
 public $LastName = '';  
 public $PhoneNu = '';

//instantiate the object created
$mc = new myClient();

//if you need to provide any information for your WCF service, this is the right place to do it

$mc->PhoneNu = “555-3456”;

//now, we need to wrap our object in the parameter we will pass
$myParam = array(‘parameterName’=>$mc);

/*Pay attention to ‘parameterName’.  This should match to your parameter name in the
   WCF service method parameter name, or the call will fail.
   Example of WCF service method:
public CompositeType GetDataUsingDataContract(CompositeType parameterName)

//The next step is to call our method with the assigned parameter
$wcf = $client->GetDataUsingDataContract($myParam);

//Get the result from the method call
$wcfResult = $wcf->GetDataUsingDataContractResult; //Note that PHP using method name with ‘Result’

//And now, we are ready to access our service object populated by WCF:
echo $wsResult->LastName;


That’s all there is to it. It will be interesting to see to see people with PHP skills participating in this discussion.

Happy programming, everyone!

Aaron J. Katz
Yuri Kasan



By Aaron Katz   Popularity  (7480 Views)