PHP communicate with WCF via SOAP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brekehan
Forum Newbie
Posts: 3
Joined: Mon Oct 26, 2009 4:57 pm

PHP communicate with WCF via SOAP

Post by brekehan »

I am on week 2.
I implemented a simple SOAP service in .NET.
I wrote a test client in .NET and it connects and works perfectly.

Now in my final solution I need PHP to connect to it. The manual has zero example code and does not explain what is returned from calling a SOAP method like a method of the client class. Some web pages seem to treat it like the return value of the service, some treat it like a PHP object (service or fault?)

I cannot get it to work no matter what I try. I've went through and mimicked several examples and each fails with exceptions or errors.

Here is my current PHP code:

Code: Select all

 
// WCF Test
try
{ 
   $client = new SoapClient("http://localhost:8001?wsdl");
 
   echo("<BR>Dumping client object:<BR>");
   var_dump($client);
   echo("<BR><BR>");
   
   echo("Dumping client functions:<BR>");
   var_dump($client->__getFunctions());
   echo("<BR><BR>");
   
   echo("Calling SOAP Add function with params 2 and 5<BR><BR>");
   $result = $client->AddPlus(5,2); 
   
   if( is_soap_fault($result) == true )
   {
      echo "There was a fault.<BR><BR>";
   }
   else
   {
      echo("Got result: " . $result . "<BR><BR>");
   }
   
   //$userID   = $records[0]->m_userID;
   //$courseID = $records[0]->m_courseID; 
   //echo("Calling SOAP NACs function with params userID=" . $userID . " courseID=" . $courseID . "<BR><BR>"); 
   //$client->UpdateNACs($userID, $courseID);
}
catch(Exception $e)
{
   echo 'Caught exception: '.  $e->getMessage(). "\n";
}
 
and the result is : "Caught exception: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Addplus'. End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 148. "

How do you call a wcf method from PHP?


P.S. are the site moderators aware that you cannot see what you are typing as this text area scrolls up with every keystroke?


EDIT: It looks like I actually got the call to go through with the following code, but I do not know how to get the result out of the returned value. I've treated it as an array and as an object and cannot get tyhe value without error:

Code: Select all

 
try
{ 
   $client = new SoapClient("http://localhost:8001/?wsdl");
 
   echo("<BR>Dumping client object:<BR>");
   var_dump($client);
   echo("<BR><BR>");
   
   echo("Dumping client functions:<BR>");
   var_dump($client->__getFunctions());
   echo("<BR><BR>");
   
   $parameters = array("addend1" => 5.0, "addend2" => 2.0);
   echo("Calling SOAP Add function with params ");
   var_dump($parameters);
   echo ("<BR><BR>");
 
   $result = $client->AddPlus($parameters); 
   
   if( is_soap_fault($result) == true )
   {
      echo "There was a fault.<BR><BR>";
   }
   else
   {
      echo("Got result: ");
      var_dump($result);
      echo("<BR><BR>");
      
      //echo($result->AddPlusResult);
   }
 
Dumping client object:
object(SoapClient)#2 (2) { ["_soap_version"]=> int(1) ["sdl"]=> resource(6) of type (Unknown) }

Dumping client functions:
array(2) { [0]=> string(44) "AddplusResponse Addplus(Addplus $parameters)" [1]=> string(53) "UpdateNACsResponse UpdateNACs(UpdateNACs $parameters)" }

Calling SOAP Add function with params array(2) { ["addend1"]=> float(5) ["addend2"]=> float(2) }

Got result: object(stdClass)#3 (1) { ["AddplusResult"]=> float(7) }
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: PHP communicate with WCF via SOAP

Post by JNettles »

How have you tried to access your results so far? Unless you specifically return a string from .NET, PHP will tend to interpret any blocks (even arrays) as existing inside of an object so even if you return an array it might be nested inside of an object. What data type is your .NET web service returning?

From what I can tell just looking at your code I think $result is an object several layers deep - have you tried $result->AddplusResult ?? If that doesn't work post your VB or C# web service function here and I'll see if I can figure out how to access what you're trying to return.


Let me know if you ever need to return whole datatables from .NET - I have a PHP class that parses them into an array of dynamically created objects.
brekehan
Forum Newbie
Posts: 3
Joined: Mon Oct 26, 2009 4:57 pm

Re: PHP communicate with WCF via SOAP

Post by brekehan »

$result->AddplusResult

works! TY!
I am just returning a double.
Post Reply