This is a repost of a post of my blog on the Dutch Microsoft Blog page. Since I moved my blog to the msdn domain they will delete my blog sooner or later and there are some posts I like to keep.
This is once again a posting in English. Yesterday and today I must have spend over 3 hours figuring out how to call an ASP.NET 2.0 webservice using PHP 5.0. Of course I tried to find some sample code somewhere and finally found some but the reasons why it took so long were trivial and not very well documented. So here is my gotchas of calling an ASP.NET webservice using a php script.
Lets start with the sample script and then I’ll tell you what things I encountered before everything worked.
function AddDevice($DeviceName,$MacAddress,$PassWord,$IPAddress, $Mask, $Gateway)
//Beware that IIS authentication can only be done through basic auth.
$username = "User";
$password = "Pass@Word!";
$client = new SoapClient("ADSConnector.wsdl", array(‘login’ => $username,
‘password’ => $password));
//The parameters of the webmethod have to be in an array. And the members of the array have to have the same name (case sensitive) as the Arguments at the webservice and
$params->DeviceName = $DeviceName;
$params->MacAddress = (string)$MacAddress;
$params->PassWord = (string)$PassWord;
$params->IPAddress = (string)$IPAddress;
$params->Mask = (string)$Mask;
$params->Gateway = (string)$Gateway;
//The return value of the webservice call is a StdClass
$result = $client->AddDevice($params);
//To get the good value from the stdClass object get the WebMethodResult member
$simpleresult = $result->AddDeviceResult;
First of all I had to call the web service authenticated and I had to figure out that php cannot authenticate using Windows Authentication. The error message that I got was:
"Fatal error: Uncaught SoapFault exception: [HTTP] Unauthorized"
The logs of my IIS server that was running my ASP webservice told me I had an Access Denied but no details at all. After fidling around a bit more I finally figured out you had to change the authentication method to Basic Authentication. As my web server and client are on a management LAN there is no real problem doing that. If you need to do this over the internet make sure you use SSL.
The second step was calling the webmethods. I would expect that if the method takes say 2 parameters that to be called something like this
It took me a while to figure out that the proper way is to put the values in an array so it has to be:
$params->$value1 = value;
$params->$value2 = value;
The next thing that hit us was that only a few of the parameters arrived at the webmethod. Thank god I was able to attach a debugger at the ASP.NET end or else it would have taken us years to figure this one out.
The reason for this problem was that the names of the members of the parameters array were not properly formatted. The ASP.NET function definition is;
string AddDevice(string DeviceName, string MacAddress, string Password, string IPAddress, string Mask, string Gateway)
And our parameters array looked like this
$params->Devicename = $DeviceName;
$params->MacAddress = $MacAddress;
$params->PassWord = $PassWord;
$params->IPAddress = $IPAddress;
$params->Mask = $Mask;
$params->Gateway = $Gateway;
Maybe you see that Devicename and PassWord are misspelled. Or at least are nog case by case the same as the names of the arguments of the AddDevice function.
After fixing this we had all the funny PHP calling ASP.NET webservice quirks ironed out and were ready to look at functionality.
I hope this post was useful and I also hope it will pop up in live search and other search engines when someone tries to find information on php and webservices and .NET or using the php soapclient.
By the way I was running the php scripts from the IIS7 webserver on my Vista laptop and getting it all to work was really a walk in the park. For more details check out this blog posting.
Have a good one.