latest posts

A couple weeks back I needed to integrate a Word Press site with a C# WCF Service. Having only interfaced with the older "classic" ASP.NET Web Services (aka asmx) nearly 6 years ago I was curious if there had been improvements to the soapClient interface inside of PHP.

Largely it was exactly as I remembered going back to 2007. The one thing I really wanted to do with this integration was have a serialized object being passed up to the WCF Service from the PHP code - unfortunately due to the time constraints this was not achievable - if anyone knows and can post a snippet I'd be curious. That being said the following sample code passes simple data types as parameters in a WCF Operation Contract from PHP.

Given the following WCF Operation Contract definition:
[OperationContract] string CreateNewUser(string firstName, string lastName, string emailAddress, string password); ]]>
Those who have done some PHP in the past should be able to understand the code below, I should note when doing your own WCF Integration, the $params->variableName needs to match casing to the actual WCF Service. In addition the object returned has the name of the WCF Service plus the "Result" suffix. [php] <?php class WCFIntegration {
     const WCFService_URL = "http://somesite.com/NameOfYourService.svc?wsdl"; public function addUser($emailAddress, $firstName, $lastName, $password) {
     try {
     // Initialize the "standard" SOAP Options $options = array('cache_wsdl' => WSDL_CACHE_NONE, 'encoding' => 'utf-8', 'soap_version' => SOAP_1_1, 'exceptions' => true, 'trace' => true); // Create a connection to the WCF Service $client = new SoapClient(self::WCFService_URL, $options); if (!$client == null) {
     throw new Exception('Could not connect to WCF Service'); }
// Set the WCF Service Parameters based on the argument values $params->emailAddress = $emailAddress; $params->firstName = $firstName; $params->lastName = $lastName; $params->password = $password; // Submit the $params object $result = $client->CreateNewUser($params); // Check the return value, in this case the WCF Service Operation Contract returns "Success" upon a succesful insertion if ($result->CreateNewUserResult === "Success") {
     return true; }
throw new Exception($result->CreateNewUserResult); }
catch (Exception $ex) {
     echo 'Error in WCF Service: '.$ex->getMessage().'<br/>'; return false; }
}
}
?> [/php] Then to actually utilize this class in your existing code: [php] $wcfClient = new WCFIntegration(); $result = $wcfClient->addUser('test@email.com', 'John', 'Doe', 'password'); if (!$result) {
     echo $result; }
[/php] Hopefully that helps someone out who might not be as proficient in PHP as they are in C# or vice-versa.