Home > PHP

Using PHP to Connect to WCF service

10. February 2010

This post will talk about some quirks as to why using the SoapClient in PHP was having troubles connecting to a WCF service created in .NET 3.0 (asp.net web service).  First off, let me start off by saying I'm not a PHP developer, and I am still trying to fully and completely understand why the solution works and the original aspect of this did not.  Let me layout what the scenario is:

  • Proxy to connect to WCF service is written in PHP 5+
  • WCF service written in .NET 3.0
  • WCF service WSDL not published
  • WCF service GET's disabled

I will not expose too much more information as it needs to be kept confidential with the folks I was working with.  However this post will explain the problems we were having.

I do want to say coming to this conclusion did take some time as it was hard to piece things together from online research so hopefully this will help others in the future.

The biggest problem we had with this was how the SOAP message was getting passed to the WCF service.  My problem was that creating the proxy class in .NET and calling the WCF service was not causing any problems or any error message of any kind. It was working about as well as it could have.  So it lead me to wonder how the SoapClient object in PHP was sending over it's constructed SOAP message. 

I started up Fiddler to construct my own HTTP POST's.  I used the SOAP message from the WCF Messaging Log file I had and I used the SOAP message from the vendor's debug output provided to me.  After comparing the SOAP messages that .NET produced and that PHP produced they were definitely different. However, to me, they both followed the standards. So in this case, why would it matter right? I still don't know. 

Here is an example of what the PHP SOAP message looked like

(note: actual URI and method names have been replaced).

 

Here is an example of what the .NET SOAP message looked like.

Now, I may be mistaken, but in the first SOAP message created by PHP's SoapClient it prefixes the GetMyDataRequest with ns1: However, in it's SOAP Envelope definition it defines what the namespace ns1: is.

At this point we were having an issue with the id parameter coming over as null. Which is a bit odd because the SOAP Message from PHP was creating a value for it in the message.  So it kind of lead me down the wrong track because I couldn't figure out why.

So I gave up and started constructing my own HTTP POST's with SOAP Messages in it using Fiddler to the WCF service.

Turns out this is a valid SOAP Message that worked

 

Notice how I got rid of the ns1: and added the xmlns attribute to the GetMyDataRequest element in the SOAP Body.

The next task was how do you get the PHP SoapClient to construct the SOAP message this way?  After more digging around the solution was found.  This link below talks about overriding the __doRequest method in the SoapClient object.

http://php.net/manual/en/soapclient.dorequest.php

The comment left by "albert at jool dot nl" in the link above was excellent.

Here is the code block from that comment:

code block from http://www.php.net/manual/en/soapclient.dorequest.php#74123


You have make your own class that extends the SoapClient object to override the __doRequest method. 

From here on out using your new class as you would the SoapClient object everything worked great!!! I hope this post can help others save hours of time when dealing with this situation.

Here is another resource with PHP and ASP.NET services as well

http://blogs.msdn.com/bramveen/archive/2009/09/03/using-php-to-connect-to-an-asp-net-2-0-webservice.aspx

 

 

 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

PHP

blog comments powered by Disqus