微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webservice Hprose TP3.2官方控制器事件补全 demo

HproseHttpServer 下载的是最新版 

注释:

/**********************************************************\
 *                                                        *
 * HproseHttpServer.PHP                                   *
 *                                                        *
 * hprose http server library for PHP5.                   *
 *                                                        *
 * LastModified: Jul 12,2014                             *
 * Author: Ma Bingyao <[email protected]>                  *
 *                                                        *
\**********************************************************/


修改官方 基类 HproseController 
修改内容

1.添加几个事件监听方法

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | copyright (c) 2006-2014 http://thinkPHP.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------

namespace Think\Controller;


/**
 * ThinkPHP Hprose控制器类
 */
class HproseController {


    protected $allowMethodList = '';
    protected $crossDomain = false;
    protected $P3P = false;
    protected $get = true;
    protected $debug = false;
    public $onBeforeInvoke = null;
    public $onAfterInvoke = null;
    public $onSendHeader = null;
    public $onSendError = null;
    public static $methods = array();


    /**
     * 架构函数
     * @access public
     */
    public function __construct() {
        //控制器初始化
        if (method_exists($this,'_initialize'))
            $this->_initialize();
        //导入类库
        vendor('Hprose.HproseHttpServer');
        //实例化HproseHttpServer
        $server = new \HproseHttpServer();
        if ($this->allowMethodList) {
            $methods = $this->allowMethodList;
        } else {
            $methods = get_class_methods($this);
            $methods = array_diff($methods,array('__construct','__call','_initialize','onBeforeInvoke','onAfterInvoke','onSendHeader','onSendError'));
        }
        self::$methods = $methods;
        $server->addMethods($methods,$this);
        if ($this->debug) {
            $server->setDebugEnabled(true);
        }
        // Hprose设置
        //是否跨域访问
        $server->setCrossDomainEnabled($this->crossDomain);
        //是否发送P3P的http头,这个头的作用是让IE允许跨域接收的Cookie
        $server->setP3PEnabled($this->P3P);
        //禁止服务器接收GET请求 参数设置为false即可
        $server->setGetEnabled($this->get);


        if (method_exists($this,'onBeforeInvoke')) {
            $server->onBeforeInvoke = '\\Think\\Controller\\HproseController::onBeforeInvoke';
        }
        if (method_exists($this,'onAfterInvoke')) {
            $server->onAfterInvoke = '\\Think\\Controller\\HproseController::onAfterInvoke';
        }
        if (method_exists($this,'onSendHeader')) {
            $server->onSendHeader = '\\Think\\Controller\\HproseController::onSendHeader';
        }
        if (method_exists($this,'onSendError')) {
            $server->onSendError = '\\Think\\Controller\\HproseController::onSendError';
        }
        // 启动server
        $server->start();
    }


    /**
     * 服务器端发布的方法调用前,onBeforeInvoke事件被触发
     * @param type $name name为客户端所调用方法名,
     * @param type $args args为方法的参数,
     * @param type $byRef byRef表示是否是引用参数传递的调用
     */
    public static function onBeforeInvoke($name = '',$args = array(),$byRef = false) {
        \Think\Log::write('$methods:' . json_encode(self::$methods));
        \Think\Log::write('event:' . __FUNCTION__ . PHP_EOL . '$name:' . json_encode($name) . PHP_EOL . '$args:' . json_encode($args) . PHP_EOL . '$byRef:' . json_encode($byRef));
    }


    /**
     * 当服务器端发布的方法被成功调用后,onAfterInvoke事件被触发
     * 当调用发生错误时,onAfterInvoke事件将不会被触发。如果在该事件中抛出异常,则调用结果不会被返回,客户端将收到此事件抛出的异常
     * @param type $name name为客户端所调用方法名,
     * @param type $args args为方法的参数,
     * @param type $byRef byRef表示是否是引用参数传递的调用
     * @param type $result 调用结果
     */
    public static function onAfterInvoke($name = '',$byRef = false,$result = '') {
        \Think\Log::write('event:' . __FUNCTION__ . PHP_EOL . '$name:' . json_encode($name) . PHP_EOL . '$args:' . json_encode($args) . PHP_EOL . '$byRef:' . json_encode($byRef) . PHP_EOL . '$result:' . json_encode($result));
    }


    /**
     * 当服务器返回响应头部时,onSendHeader事件会被触发
     */
    public static function onSendHeader() {
        
    }


    /**
     * 当服务器端调用发生错误,或者在onBeforeInvoke、onAfterInvoke事件中抛出异常时,该事件被触发
     * 您可以在该事件中作日志记录,但该事件中不应再抛出任何异常
     * @param type $error
     */
    public static function onSendError($error) {
        \Think\Log::write('$methods:' . json_encode(self::$methods));
        \Think\Log::write('event:' . __FUNCTION__);
        \Think\Log::write('$error:' . json_encode($error));
    }


    /**
     * 魔术方法 有不存在的操作的时候执行
     * @access public
     * @param string $method 方法名
     * @param array $args 参数
     * @return mixed
     */
    public function __call($method,$args) {
        \Think\Log::write('$methods:' . json_encode(self::$methods));
        \Think\Log::write('event:' . __FUNCTION__);
    }


}


server


<?PHP

/**
 * Webservice
 * @author Administrator
 */

namespace Extend\Controller;

use Think\Controller\HproseController;

class HproseServerController extends HproseController {

    /**
     * 初始化
     */
    public function _initialize() {
//        $this->allowMethodList = 'test';
        $this->debug = false;
        $this->get = true;
    }

    public function index() {
        
    }

    public function test($str = '') {
        return "test" . $str;
    }

    public function test1($str = '') {
        return "test1" . $str;
    }

    public function json() {
        $arr = array('json' => 'jsonvalue',1 => 'jsonint','str' => 'strvalue');
        return json_encode($arr);
    }

}



client

    public function hprose() {
        $fun = I('get.fun','test');
        vendor('Hprose.HproseHttpClient');
        $url = '';//server url
        $client = new \HproseHttpClient($url);
        echo $client->$fun("Hprose");
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐