/**
* 文件名 : config.php
* 用途 : Hessian配置文件
*
* @package system.core.code applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
// 根目录
define( 'PATH',dirname(__FILE__) . DIRECTORY_SEPARATOR );
// Hessian Url地址
define( 'HESSIAN_URL','http://qx.com/server.php' );
// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>
/**
* 文件名 : server.php
*
* 参考资料 :
* 1.http://hessian.caucho.com/ ( Hessian主页 )
* 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
* 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
* 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
*
* @author wubaiqing
* @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() {}
/**
* 商品详细信息APi接口
* @param string $title 标题
* @param int $price 价格
*/
public function goodsInfomationApi( $title,$price ) {
$price = (int) $price;
return '
标题:' . $title . '
价格:'.$price;
}
}
$server = new HessianService( new HessianServer() );
//$server->displayInfo();
$server->handle();
// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>
/**
* 类名 : HessianApi
*
* 参考资料 :
* 1.http://hessian.caucho.com/ ( Hessian主页 )
* 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
* 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
* 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
*
* @author wubaiqing
* @package system.core applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
class HessianApi
{
/**
* @var string 接口地址
*/
private $_url = NULL; /**
* @var result 句柄
*/
private $_handle = NULL;
/**
* @var array 存放单例模式数组
*/
private static $_objects = array();
/**
* 设置URL地址
* 实例化HessianClient类
* 参数 : (1) url地址,2
*
* 2.Java调用字段
* @param string $url
*/
public function __construct( $url )
{
$this->setUrl( $url );
$handler = new HessianClient ( $this->getUrl (),$this->getOptions () );
$this->setHandler ( $handler );
}
/**
* @return result $_handle 句柄
*/
public function getHandler() {
return $this->_handle;
}
/**
* 设置句柄
* @param result $_handle
*/
public function setHandler($_handle) {
$this->_handle = $_handle;
}
/**
* 获取URL地址
*/
public function getUrl() {
return $this->_url;
}
/**
* 设置URL地址
* @param string $url
*/
public function setUrl($url) {
$this->_url = $url;
}
/**
* typeMap映射Java等平台对象
* @return array
*/
public function getOptions() {
return array (
'version' => 1,
'saveRaw' => TRUE,
'typeMap' => array(
'JavaNullPointException' => 'java.lang.NullPointerException',
'StackTraceElement' => 'java.lang.StackTraceElement')
);
}
/**
* 记录接口调用信息
* @param string $method 调用的方法
* @param string $returnMsg 需要记入log的文字信息
*/
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');
}
/**
* 静态工厂方法,生成单个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
?>
/**
* 类名 : Goods
* 继承类 : HessianApi
* 用途 : 调用server.php方法
*
* @author wubaiqing
* @package system.core.code applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
class Goods extends HessianApi
{
/**
* 设置接口地址
* @param string $url
*/
public function __construct( $url ) {
parent::__construct( $url );
}
/**
* 获取商品信息
* 调用server.php文件中的goodsInfomationApi方法
* @param string $title 标题
* @param string $title 价格
*/
public function getGoodsInfomation( $title,$price )
{
// 如果调用java平台的hessian服务 需要指定你传递参数的类型,特别是整形和字符串.
$price = (int) $price; $result = $this->getHandler()->goodsInfomationApi( $title,$price );
$this->resultLog( 'getGoodsInfomation','访问接口,但接口没有进行逻辑验证.');
return $result;
}
}
// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>
/**
* 文件名 : index.php
*
* 参考资料 :
* 1.http://hessian.caucho.com/ ( Hessian主页 )
* 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
* 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
* 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
*
* @author wubaiqing
* @package system.core applied to the whole site
* @copyright Copyright (c) 2012
* @since 1.0
*/
require_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR .'config.php' );
// Hessian 扩展及配置文件
require_once ( PATH . 'extensions/HessianPHP/HessianClient.php' );
require_once ( PATH . 'class/HessianApi.php' );
// 调用 server.php 方法
require_once ( PATH . 'class/Goods.php');
// 请求接口获取数据
$goods = new Goods( HESSIAN_URL );
// 设置商品标题,价格.
$title = '北京移动充值平台';
$price = '50';
// 请求Hessian协议
$goodsInfo = $goods->getGoodsInfomation( (string) $title,(int) $price );
// 打印请求结果
echo ( $goodsInfo );
// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>