• uc_client调用代码隔离方案


    本文已经作了更为详细的解析:请移步 UCENTER UC_CLIENT调用时代码冲突解决方案:改用HTTP方式调用,实现代码隔离 http://www.snblog.cn/archives/70

    前些时间用ucenter来做一个用户登录系统,项目本身使用的是ThinkPHP框架,由于早期的PHP没有命名空间的原因,于是Ucenter与ThinkPHP的代码产生了冲突,具体是什么冲突我没有去细究,相信也没有这个必要,反正我是证明了二者的代码存在冲突就好了。

    而我又不得不同时使用Ucenter和ThinkPHP,因此我需要一个解决方案,于是很自然地想到了,把对Ucenter的调用做成HTTP Service的形式,让ThinkPHP与Ucenter通过HTTP协议来进行通信,这就把二者的运行环境隔离开来了。

    由于比较简单,下面直接上代码,不明白的可以回复评论提问。

    <?php
    // UClient 中间件通讯类
    class UClientApi{
    private function __call( $method, $args ){
    // 使用 curl 扩展进行 HTTP 通信
    // ThinkPHP 项目配置文件需作以下配置:
    // UCLIENT_URL UClient HTTP 服务的 URL
    // UCLIENT_KEY UClient HTTP 服务的密钥


    $data = 'UCLIENT_KEY='.urlencode(C('UCLIENT_KEY')).'&method='.urlencode($method).'&args='.urlencode(base64_encode(serialize($args)));

    $uclient_handle = curl_init( C('UCLIENT_URL') );

    $opts = array(
    CURLOPT_HEADER =>false,
    CURLOPT_RETURNTRANSFER =>true,
    CURLOPT_POST =>true,
    CURLOPT_POSTFIELDS =>$data
    );

    curl_setopt_array( $uclient_handle, $opts );
    $result = curl_exec( $uclient_handle );

    return unserialize(base64_decode($result));

    }
    }

    感叹一下PHP有多高效。

    <?php
    // Web Service 接口文件
    ob_start();

    include './config.inc.php';
    include './uc_client/client.php';

    // 检查密钥,认证访问身份
    if ( empty($_POST['UCLIENT_KEY']) || $_POST['UCLIENT_KEY'] != UCLIENT_KEY) exit('Access denied.');

    // 检查调用的api是否存在
    $method = 'uc_'.$_POST['method'];
    if ( !function_exists( $method ) ) exit('Method is not exists.');

    $args = unserialize(base64_decode($_POST['args']));
    $exec_string = '$result='+$method.'(';
    for ($i = 0; $i < count($args); $i++) $exec_string += '$args['+$i+']';
    $exec_string += ');';

    eval($exec_string);

    ob_clean();
    exit( base64_encode(serialize($result)) );
  • 相关阅读:
    关于 0xCCCCCCCC
    extern "C" 和 DEF 文件.
    Visual Studio 编译纯 C 项目的方法
    Virtual Box 增加虚拟硬盘容量
    Java三种代理模式:静态代理、动态代理和cglib代理
    java集合框架综述
    JsonAutoDetect注解找不到错误
    SpringBoot整合Redis
    Spring重要注解@ControllerAdvice
    SpringBoot整合+logback日志配置
  • 原文地址:https://www.cnblogs.com/acpp/p/2426965.html
Copyright © 2020-2023  润新知