Consuming a Hessian web service
To start consuming remote Hessian web services all you need to do is:
- Include or require the file HessianClient.php
- Create a HessianClient object passing the url of the service
- Call methods
This is an example code that creates a proxy to a remote service, calls several methods and prints the results:
include_once 'HessianClient.php';
$testurl = 'http://localhost:8080/resin-doc/protocols/tutorial/hessian-add/hessian/math';
$proxy = &new HessianClient($testurl);
echo $proxy->add(2,5);
echo $proxy->sub(2,5);
echo $proxy->mul(2,5);
echo $proxy->div(2,5);
After a call you can check the value of the Hessian::error() function:
if(Hessian::error()) {
// do something with the error
}
And for PHP5 you might want to enclose the code inside a try..catch block, like this:
try {
echo $proxy->add(2,5);
} catch (Exception $e) {
// do something with the exception
}
Publishing a Hessian web service
You have to create a script in the web server following this steps:
- Include or require the file HessianService.php.
- Create a HessianService wrapper object
- Register a previously created or new object by calling registerObject().
- Execute the service() method.
Thus, for example, if we want to publish a calculator service that is compatible with the previous example, we have to create a class something like this:
class Math{
function add($n1,$n2) {
return $n1+$n2;
}
function sub($n1,$n2) {
return $n1-$n2;
}
function mul($n1,$n2) {
return $n1*$n2;
}
function div($n1,$n2) {
return $n1/$n2;
}
}
Then create the service wrapper and register a Math object to publish it:
include_once 'HessianService.php';
$wrapper = &new HessianService();
$wrapper->registerObject(new Math);
$wrapper->service();
That's it. Now we have successfully published a Hessian web service from a common PHP object. The url of the service is the same url of the script.
If you try to access the url using a web browser, you will get a 500 error because Hessian requires POST to operate.