【问题标题】:Getting "error"=>"Array" array response from python JSON server and PHP client从python JSON服务器和PHP客户端获取“error”=>“Array”数组响应
【发布时间】:2014-10-05 07:13:25
【问题描述】:

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

我不确定我做错了什么,因为我已经测试了服务器并且在我不使用 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() 只是引发异常 “请求错误:数组”

编辑2: 解决了问题,原来我请求的方法不正确。我忘了我把名字改成了stop()。

逻辑错误...

【问题讨论】:

    标签: php python json client rpc


    【解决方案1】:

    构造你的对象,将调试标志设置为 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;
        }
    

    【讨论】:

    • 当我查看代码时,我发现了同样的事情。可能是reuest在某种程度上是错误的,导致服务器返回错误?我已经测试了服务器,它在不使用 PHP 时运行良好。
    • 检查python服务器中的请求?检查 curl 和 json-rpc 之间的请求有什么区别。您可以从问题中的服务器粘贴一些服务器代码/调试信息。
    猜你喜欢
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多