从python JSON服务器和PHP客户端获取“error”=>“Array”数组响应

时间:2014-10-05 07:13:25

标签: php python json client rpc

我在python中编写了一个简单的JSON RPC服务器作为python程序的接口。当我使用curl从它手动构造JSON请求时,服务器工作正常。当我从这里使用JSON库时:http://jsonrpcphp.org/我只得到一个错误响应“数组”作为回报。

我不确定我做错了什么,因为我测试了服务器,当我不使用PHP库时它就可以工作。

我已经检查过,内容类型设置为“application / json”。

继承PHP代码:

$server= new jsonRPCClient("http://$serveraddress:$port");


try {
    echo($server->do_stop());
} catch (Exception $e) {
    echo "caught exception: ", $e->getMessage() ,"<br />";
}

编辑:

这是$ server对象的print_r(localhost应该在它之前有http://但是我不能把它放进去):

jsonRPCClient Object ( [debug:jsonRPCClient:private] => [url:jsonRPCClient:private] => localhost:3000[id:jsonRPCClient:private] => 1 [notification:jsonRPCClient:private] => [proxy] => ) 

do_stop()只会引发异常     “请求错误:数组”

EDIT2: 解决了这个问题,结果发现我要求的方法不正确。我忘了我把名字改成了停止()。

逻辑错误......

1 个答案:

答案 0 :(得分:1)

构造ur对象,将debug标志设置为true

 $server= new jsonRPCClient("http://$serveraddress:$port", true);

阅读json rpc客户端的source code,似乎错误来自你的服务器:

    // final checks and return
    if (!$this->notification) {
        // check
        if ($response['id'] != $currentId) {
            throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
        }
        if (!is_null($response['error'])) {
            throw new Exception('Request error: '.$response['error']);
        }

        return $response['result'];

    } else {
        return true;
    }