A PHP variable has a value and a type. In most practice cases we consider only about the value of the variable. But there may be times we have to consider the type of the variable as well.

For an example value NULL (or 0 or whatever of following you likeĀ  to call it) can be assigned to a variable with different types like this.

$x = NULL; NULL data type. Yea it is the real NULL.
$x = 0; Integer data type.
$x = 0.0; Float data type.
$x = FALSE; Boolean data type.
$x = “”; String data type.

You can check the operator ‘===’ (triple equal operator) to check equality of both their values and types simultaneously.

You can have a good idea about this by looking at the following piece of code.

// lets check the equality of NULL and 0 with
// double equal operator
if(NULL == 0) {
	echo "NULL == 0 is TRUE</br>";
}
else {
	echo "NULL == 0 is FALSE</br>";
}

// now lets check the equality of NULL and 0 with
// triple equal operator
if(NULL === 0) {
	echo "NULL === 0 is TRUE</br>";
}
else {
	echo "NULL === 0 is FALSE</br>";
}

This will eventually print the following result.

NULL == 0 is TRUE
NULL === 0 is FALSE

PHP variables including class variables get the NULL value and NULL type by default. So if no one assign them a value it remains NULL.

So in a case you have to check for unused variables (or for “NULL”-ness of a variable) you should always use the triple equal operator (===) with NULL token. Otherwise you may mistakenly treat not NULL things like ‘0′ or empty string (”") as null that may have been valid data for your application.

Web Service can response with a Fault in 2 occasions.

  1. Fault send by the web service framework. (E.g. Invalid authentication, invalid signature found)
  2. Fault send by the user business logic.

There is a slightly difference in the content of SOAP 1.1 and SOAP 1.2. But they mainly contain the following elements.

  1. Code - A code to represent the classification of the fault. Possible fault codes can be found, http://www.w3.org/TR/soap12-part1/#faultcodes
  2. Reason - A human readable details of the reason.
  3. Details - More information about the details, mostly supposed to be read by the client application.
  4. Role - Indicates which SOAP header caused the fault. This is very rarely used in faults send from the business logic.

Sending SOAP Faults

In WSF/PHP you have the WSFault class to deal with SOAP faults. You can send a fault in your service logic by throwing an instance of WSFault class like this.

/**
 * divide mathematical operation
 * @param int $dividend
 * (maps to xs:int)
 * @param int $divisor
 * (maps to xs:int)
 * @return float $result
 * (maps to xs:float)
 */
function divide($dividend, $divisor)
{
	// dividing from 0 is invalid, we wil *throw* fault in such cases..
	if($divisor == 0) {
		throw new WSFault("Sender", "dividing from 0 is invalid");
	}

	$result = (float)$dividend/$divisor;

	return array("result" => $result);
}

Here I have throw an WSFault whenever I encounter my divisor is zero. And the WSF/PHP will take care of building the SOAP message according to the given version (default is to SOAP 1.2) and send back to the client.

Handling SOAP Faults

Similar to the service, client API also treat the SOAP fault as an instance of WSFault. So whenever you do a web service request, put inside try, catch block so you can catch exception in case of fault is received. Here is an example of handling fault while calling the divide operation in the above example.

// creating the client, we retrieved the wsdl from service url + ?wsdl
$client = new WSClient(array(
			"wsdl" => "http://localhost/myblog/fault_service.php?wsdl"));

$proxy = $client->getProxy();

try {
	// calling the operation
	$response = $proxy->divide(array("dividend" => 5, "divisor" => 0));

	// printing the result
	echo $response["result"];

} catch(Exception $e) {

	// if the instance is WSFault we print the code and the reason
	if ($e instanceof WSFault) {
        printf("Soap Fault Reason: %s\n", $e->Reason);
        printf("Soap Fault Code: %s \n", $e->Code);
	} else {
		printf("Message = %s\n",$e->getMessage());
	}
}

As you can see WSF/PHP covers the complexity of building and handling SOAP faults, Rather it gives you an API with the use of PHP Exception Construct that you already familiar with.

You can watch a Screencast on How to Consume a Web Service Using WSF/PHP from WSO2 Oxygent Tank developer portal.

There I have presented the steps you need to follow to consume a web service. I choose US National Digital Forecasting database Web Service as my example service to write the demo client.

This screencast contains,

  1. Where to find the Service documentation + WSDL and what are the information available in there.
  2. How to generate the PHP client from the WSDL using wsdlphp tool.
  3. What is in the php file generated from the wsdl2php tool and how you select the required operations to invoke.
  4. How to fill the input parameter for the web service operation
  5. How to handle the response returned from the service.
  6. Finally It shows some different ways the data that you are extracting from the service, can be presented to your user.