Recent Changes - Search:

Documents

Community

Related Projects

Powered by PmWiki

WritingServers

Suppose now we want to publish the same userService service in PHP. In HessianPHP, objects are converted to Hessian web services by registering them in a wrapper. So for this example we need to have 2 classes, User and UserService, they will be in another file called UserClasses.php.

File: UserClasses.php

// data transfer object 

class User { 
    var $name ; 
    var $lastName ; 
    var $code ; 

    function User( $name = '' , $lastName = '' , $code = 0 ){ 
          $this->name = $name ; 
          $this->lastName = $lastName ; 
          $this->code = $code ; 
    } 
}   

// Example service with fixed data. In the real world 
// this class will do other things and data should come from a database 

class UserService { 
    var $users = array(); 

    function UserService(){ 
        $this->users['hpotter'] = &new User( "Harry" , "Potter" , 321 ); 
        $this->users['ymalmsteem'] = &new User( "Yngwie" , "Malmsteen" , 777 );
        $this->users['mgomez'] = &new User( "Manolo" , "Gómez" , 123 ); 
    } 

    function getUsers(){ 
        return $this->users ; 
    } 

    function &findUser($name){ 
        if(isset($this->users[$name])) 
            return $this->users[$name]; 
        return null ; 
    } 
} 

Now we are ready to publish UserService with HessianPHP. We need to create a PHP script file where we create a service wrapper object:

File: userService.php

include_once( 'HessianService.php' ); 
$wrapper = &new HessianService(); 

//Now we create a UserService object, register it with the service wrapper 
//and call service() to listen for incoming calls: 

$userService = &new UserService(); 
$wrapper->registerObject($userService); 
$wrapper->service(); 

And that's it! In order to consume the service we have just created all you need to do is create a Hessian client with the url of userService.php

// User and UserService are defined in another file called 
// UserClasses.php

include_once( 'UserClasses.php' ); 
include_once( 'HessianService.php' ); 
$wrapper = &new HessianService(); 
$userService = &new UserService(); 
$wrapper->registerObject($userService); 
$wrapper->service(); 

Please note that if the serviced object prints anything to the screen BEFORE the call to service(), the reply will be corrupted!

For example suppose we decide to debug the method findUser in UserService to print every time the method is called, such as this:

...
function &findUser($name){ 
    echo "searching $name" ; 
    if(isset($this->users[$name])) 
       return $this->users[$name]; 
    return null ; 
} 
...

When you execute the client script, a HessianPHP client will throw this (again with error level at HESSIAN_TRIGGER_ERROR):

Fatal error : Hessian Parser, Malformed reply: expected r in C:\Apache\htdocs\ HessianPHP\hessian.php on line 321

Another platform clients will probably complain too. This is because HessianPHP uses the standard output to send the binary data reply to the client.

If you try to debug the service with the browser you will get a 500 error because Hessian requires POST to work, so the only advise for now is debug your service objects before publishing them and make sure the script is well formed so the PHP interpreter doesn't print warnings or notices before a call to service().

Edit - History - Print - Recent Changes - Search
Page last modified on December 21, 2005, at 02:54 AM

PmWiki can't process your request

Cannot acquire lockfile

We are sorry for any inconvenience.