What is Hessian
Hessian is an Open-source remote communication protocol provided by the Caucho.
Using binary RPC protocol, based on HTTP transmission, the server side does not have to open the firewall port in another.
The specification of the Protocol is public and can be used in any language.
Use client/server mode.
The requestor is a client, and the service provider is a server.
The client invokes the process to send a call message with process parameters to the service process, and then wait for the answer message.
On the server side, the process stays dormant until the call information arrives.
When an invocation message arrives, the server obtains the process parameters, evaluates the result, sends a reply message, and then waits for the next call message, and finally, the client calls the process to receive the reply message,
Gets the process result, and then the call execution continues.
Hessian protocol Work Flow chart
Client program request Service-side function
1. Invokes the client handle and executes the transfer parameters.
2. Call the local system kernel to send network messages.
3. Message delivery to remote host.
4. The server handle gets the message and gets the parameters.
5. Perform remote procedures.
Service-side function returns results to client
1. The execution process returns the result to the server handle.
2. The server handle returns the result, calling the remote system kernel.
3. The message is sent back to the local host.
4. Client handles receive messages from the kernel.
5. The client receives the data returned by the handle.
Source Code Explanation
1. Refer to the configuration file, including the site root directory, and the Hessian address.
Copy Code code as follows:
<?php
/**
* FileName: config.php
* Use: Hessian configuration file
*
* @package System.core.code applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
root directory
Define (' PATH ', DirName (__file__). Directory_separator);
Hessian URL Address
Define (' Hessian_url ', ' http://qx.com/server.php ');
Ide:zend Studio 9.0
IDE Extension:toggle Vrapper
?>
2. Configure the service side.
Copy Code code as follows:
<?php
/**
* FileName: server.php
*
Resources
* 1.http://hessian.caucho.com/(Hessian homepage)
* 2.http://hessianphp.sourceforge.net/(Hessian PHP)
* 3.http://sourceforge.net/projects/hessianphp/(Hessian php Open source)
* 4.http://baike.baidu.com/view/1859857.htm (single case mode)
*
* @author wubaiqing <xinxiangmo@gmail.com>
* @package System.core applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
Require_once (DirName (__file__). Directory_separator. ' config.php ');
Require_once (PATH. ' extensions/hessianphp/hessianservice.php ');
Class Hessianserver
{
Public Function __construct () {}
/**
* Product Details API interface
* @param string $title title
* @param int $price Price
*/
Public Function Goodsinfomationapi ($title, $price) {
$price = (int) $price;
Return '
}
}
$server = new Hessianservice (new Hessianserver ());
$server->displayinfo ();
$server->handle ();
Ide:zend Studio 9.0
IDE Extension:toggle Vrapper
?>
3. The DisplayInfo method in the Hessianservice class can be used to see how many communication methods are opened.
If you want to build the server to use the handle method, such as the emergence of Hessian Requires post prompts, the server has been built successfully.
4. Package Hessian Interface
Copy Code code as follows:
<?php
/**
* Class Name: Hessianapi
*
Resources
* 1.http://hessian.caucho.com/(Hessian homepage)
* 2.http://hessianphp.sourceforge.net/(Hessian PHP)
* 3.http://sourceforge.net/projects/hessianphp/(Hessian php Open source)
* 4.http://baike.baidu.com/view/1859857.htm (single case mode)
*
* @author wubaiqing <xinxiangmo@gmail.com>
* @package System.core applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
Class Hessianapi
{
/**
* @var String Interface address
*/
Private $_url = NULL;
/**
* @var result handle
*/
Private $_handle = NULL;
/**
* @var array to store a single example pattern
*/
private static $_objects = Array ();
/**
* Set URL address
* Instantiate Hessianclient Class
* Parameters: (1) URL address, 2
*
* 2.Java Call Field
* @param string $url
*/
Public function __construct ($url)
{
$this->seturl ($url);
$handler = new Hessianclient ($this->geturl (), $this->getoptions ());
$this->sethandler ($handler);
}
/**
* @return Result $_handle handle
*/
Public Function GetHandler () {
return $this->_handle;
}
/**
* Set Handle
* @param result $_handle
*/
Public Function SetHandler ($_handle) {
$this->_handle = $_handle;
}
/**
* Get URL address
*/
Public Function GetUrl () {
return $this->_url;
}
/**
* Set URL address
* @param string $url
*/
Public Function SetUrl ($url) {
$this->_url = $url;
}
/**
* Typemap map Java and other platform objects
* @return Array
*/
Public Function getoptions () {
Return Array (
' Version ' => 1,
' Saveraw ' => TRUE,
' Typemap ' => Array (
' Javanullpointexception ' => ' java.lang.NullPointerException ',
' Stacktraceelement ' => ' java.lang.StackTraceElement ')
);
}
/**
* Record interface invocation information
* @param string $method Called Method
* @param string $RETURNMSG text information to log in
*/
Public Function Resultlog ($method, $RETURNMSG)
{
$logPath = PATH. ' /runtime/hessian/';
if (!is_dir ($logPath)) {
mkdir ($logPath, 0777);
}
error_log Date (' Ymd h:i:s ', Time ()). '|' . $method. '|' . $RETURNMSG. " \ n ", 3, $logPath. Date (' y-m-d ', Time ()). '. log ');
}
/**
* Static Factory method, generating a unique instance of a single URL
* @param string $url
*/
public static function Start ($url)
{
$key = MD5 ($url);
if (Isset (self::$_objects[$key])) {
return self::$_objects[$key];
}
self::$_objects[$key] = new Hessianapi ($url);
return self::$_objects[$key];
}
}
Class Javanullpointexception extends Exception {}
Class Stacktraceelement extends Exception {}
Ide:zend Studio 9.0
IDE Extension:toggle Vrapper
?>
5. Encapsulate client request method, inherit Hessianapi class
Copy Code code as follows:
<?php
/**
* Class Name: Goods
* Inheriting class: Hessianapi
* Use: Invoke Server.php method
*
* @author wubaiqing <xinxiangmo@gmail.com>
* @package System.core.code applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
Class Goods extends Hessianapi
{
/**
* Set interface Address
* @param string $url
*/
Public function __construct ($url) {
Parent::__construct ($url);
}
/**
* Get product information
* Call the Goodsinfomationapi method in the server.php file
* @param string $title title
* @param string $title price
*/
Public Function Getgoodsinfomation ($title, $price)
{
If you call the Hessian service on the Java platform, you need to specify the type of parameter you pass, especially the shaping and string.
$price = (int) $price;
$result = $this->gethandler ()->goodsinfomationapi ($title, $price);
$this->resultlog (' getgoodsinfomation ', ' access interface, but the interface is not logically validated. ');
return $result;
}
}
Ide:zend Studio 9.0
IDE Extension:toggle Vrapper
?>
6. Modify index.php to request service-side interface
Copy Code code as follows:
<?php
/**
* FileName: index.php
*
Resources
* 1.http://hessian.caucho.com/(Hessian homepage)
* 2.http://hessianphp.sourceforge.net/(Hessian PHP)
* 3.http://sourceforge.net/projects/hessianphp/(Hessian php Open source)
* 4.http://baike.baidu.com/view/1859857.htm (single case mode)
*
* @author wubaiqing <xinxiangmo@gmail.com>
* @package System.core applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
Require_once (DirName (__file__). Directory_separator. ' Config.php ');
Hessian Extensions and configuration files
Require_once (PATH. ' extensions/hessianphp/hessianclient.php ');
Require_once (PATH. ' class/hessianapi.php ');
Calling the Server.php method
Require_once (PATH. ' class/goods.php ');
Request interface Fetch data
$goods = new Goods (hessian_url);
Set the item title, price.
$title = ' Beijing mobile recharge Platform ';
$price = ' 50 ';
Request Hessian Protocol
$goodsInfo = $goods->getgoodsinfomation ((String) $title, (int) $price);
Print Request Results
Echo ($goodsInfo);
Ide:zend Studio 9.0
IDE Extension:toggle Vrapper
?>