zoukankan      html  css  js  c++  java
  • jsonRPC

    jsonRPC重新整理:


    __call

    php内置函数:

    当请求一个对象的方法不存在是会调用此方法:

    /*
    *@param string $method 请求方法名称。
    *@param array $params 请求方法的参数。
    */
    function __call ($method,$params);


    server.php(服务器)

    <?php
    header("Content-Type:text/html;charset=utf-8");
    $request = file_get_contents('php://input');
    $request=json_decode($request);
    
    $func=$request->func;
    $param=$request->param;
    
    print_r($func);
    print_r($param);
    
    function register(){
    
    }
    
    /*自定义api接口类*/
    class API{
        /**
         * 当请求的方法不存在
         */
        function __call($func,$param){
            throw new Exception($func.'方法不存在!');
        }
        /**
         * 获取接口名称
         */
        public function getApiName(){
            echo  "ApiName:test";
        }
        /**
         *可以定义更多的函数接口。
         */
        public  function  andSoOn(){
            //do nothing
        }
    }
    
    $api=new API();
    call_user_func_array(array($api,$func),$param);
    call_user_func_array函数说明:
       调用回调函数,比如说,小明要买鸡蛋,他可能买的是土鸡蛋或者是超市鸡蛋。这是这个函数就可以派上用场了。

    Client.php (客户端)

    header("Content-Type:text/html;charset=utf-8");
    $request=array(
        'func'=>'getApiName',
        'param'=>array(
        ),
    );
    $request=json_encode($request);
    $opts = array ('http' => array (
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'content' => $request,
    ));
    $context  = stream_context_create($opts);
    
    if ($fp = fopen('http://localhost/web/test/jsonRPC/xx.php', 'r', false, $context)) {
        $response = '';
        while($row = fgets($fp)) {
            $response.= trim($row)."
    ";
        }
        echo $response;
    }

     

  • 相关阅读:
    .NET Core开发日志——从搭建开发环境开始
    通过Docker构建TensorFlow Serving
    通过Jenkins在IIS上布署站点
    RabbitMQ in Depth札记——AMQ协议
    Web API对application/json内容类型的CORS支持
    ng-book札记——路由
    ng-book札记——HTTP
    ng-book札记——依赖注入
    UVA
    POJ
  • 原文地址:https://www.cnblogs.com/canbefree/p/3948769.html
Copyright © 2011-2022 走看看