First we create a proxy that points to the url of the published Java based service.
File: testclient.php
include_once( 'HessianClient.php' );
$testurl = 'http://localhost:8080/hessian/service/userService';
$proxy = &new HessianClient($testurl);
Now we need to perform remote calls to a method named findUser() with a string argument, this method returns a User object:
$user = $proxy->findUser('mgomez');
Ok, but wait, when we execute the script we get the following if error level is at HESSIAN_TRIGGER_ERROR: (This only applies to PHP 4)
Fatal error : Hessian Fault: Service fault: The service has no method named: finduser, dump:
array (
'code' => 'NoSuchMethodException'
)
in C:\Apache\htdocs\HessianPHP\Hessian.php on line 344
So what happened? Because of PHP 4 internals, dynamically called methods are lowercased at runtime so you are really calling "finduser" and not "findUser".
But there's a way out. Before calling the method, you have to declare the remote method's name so HessianPHP can resolve it's name before sending the call, like this:
Hessian::remoteMethod($url,'findUser');
This way you can execute the call with no problem. This behavior is documented in the FAQ and note that PHP 5 is not affected.
NOTE: These function use to be in the proxy object itself but it was moved into a general configuration class named just Hessian, this to leave the clean proxy object completely.
Now we execute the call and print_r() the results for debugging:
include_once( 'HessianClient.php' );
$testurl = 'http://localhost:8080/hessian/service/userService' ;
$proxy = &new HessianClient($testurl);
Hessian::remoteMethod($testurl,'findUser');
$user = $proxy->findUser('mgomez');
print_r($user);
We get the following:
Array (
[name] => Manolo
[lastName] => Gómez
[code] => 123
)
HessianPHP returns an associative array where indexes are the object field names. If want to get an object of the right class, you have to declare a class and if the type is known you have to map the fully qualified name of the remote class to a local type using the mapRemoteType() function, like this:
Hessian::mapRemoteType('service.User','User');
Note that lastName field contains the accentuated character ó. Hessian spec states that strings are utf-8 encoded so the protocol can handle special characters seamlessly.