Custom Faults in WSF/PHP

Monday, February 4, 2008

Custom faults is very much required in cases you have to report exception from your business logic. It is equal to throwing and catching exception in Java, or PHP5 except that one who throws the exception is in one side and the one who catches would be in somewhere else in a network.

WSF/PHP has bound this custom faults in to a PHP language constructs, so PHP developers will not need to worry about web service messages and their formats. Here is a simple example of how you can throw and catch custom faults.

your server side will be

function greet($name)
{
if(is_my_enemy($name)) {

throw new WSFault("sender", "you are not my friend");
return "<xml>{$name}</xml>";

}

$service = new WSService(array("operations" => "greet"));

$service->reply();


You will handle the fault in the client side with this code,

$client = new WSClient(array("to" => "http://localhost/url/to/above/service.php"));
try{

$res = $client->request($req);
}
catch(Exception $e)

{
if($e instanceof WSFault) {
just_report_error($e->reason);
}

}

No comments: