我正在尝试实现一个简单的 JSON-RPC 服务器。客户端部分由 jquery.jsonrpc 处理。这似乎工作正常,发送 JSON-RPC 消息作为发布消息的有效负载。
我的 JSON-RPC“服务器”目前只想回显数据,以便在 FireBug http 响应中看到结果。
代码是这样的:
<?php
class jsonrpc {
var $requestData;
function jsonrpc() {
if (isset($_SERVER["REQUEST_URI"]) && isset($_SERVER["REQUEST_METHOD"])) {
if (isset($_SERVER["CONTENT_LENGTH"]) && $_SERVER["CONTENT_LENGTH"] > 0) {
$this->requestData = "";
$httpContent = fopen("php://input", "r");
echo "httpcontent=".$httpContent;
while ($data = fread($httpContent, 1024)) {
$this->requestData .= $data;
}
fclose($httpContent);
}
}
echo "jsonrpc::jsonrpc()\n";
}
}
?>
响应选项卡显示:
POST http://api.localhost/index.php?tm=1317246797964 200 OK 6ms
<br />
<b>Warning</b>: fopen("php://input", "r") - No error in <b>C:\Develop\ZeroSumGames\api\htdocs\rpc.php</b> on line <b>9</b><br />
httpcontent=<br />
<b>Warning</b>: fread(): supplied argument is not a valid File-Handle resource in <b>C:\Develop\ZeroSumGames\api\htdocs\rpc.php</b> on line <b>11</b><br />
<br />
<b>Warning</b>: fclose(): supplied argument is not a valid File-Handle resource in <b>C:\Develop\ZeroSumGames\api\htdocs\rpc.php</b> on line <b>14</b><br />
jsonrpc::jsonrpc()
Object { error="Internal server error", version="2.0"}
我可以看到那里有数据,因为在 FireBug 请求标头选项卡上我可以看到:
Host api.localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept application/json, text/javascript, */*
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/json; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://api.localhost/index.html
Content-Length 72
我也可以在帖子标签上看到这个:
{"jsonrpc":"2.0","method":"example.method.name","id":1,"params":[1,2,3]}
我的服务器对于 file_get_contents (4.2.2)来说太旧了,但是我在网上找到的替换功能与我上面写的(或多或少)相同,并且还有关于打开php://input
.
所以我的问题是为什么我不能打开 php://input 进行阅读?