如何获取请求正文?
我正在尝试实现一个简单的 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 进行阅读?
I am trying to implement a simple JSON-RPC server. The client part is handled by the jquery.jsonrpc. This seems to work fine, sending a JSON-RPC message as the payload of a post message.
My JSON-RPC 'server' currently just wants to echo the data so I can see the result in the FireBug http response.
The code is this:
<?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";
}
}
?>
And the Response tab shows:
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"}
I can see there is data there because on the FireBug request headers tab I can see this:
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
And also I can see this on the post tab:
{"jsonrpc":"2.0","method":"example.method.name","id":1,"params":[1,2,3]}
My server is too old for file_get_contents (4.2.2) but the replacement functions I have found on the net internally do the same as what I have written above (more or less) and also have the issue regarding opening of php://input
.
So my question is why can't I open php://input for reading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将本地计算机升级到 4.4.2,工作正常,所以我猜这是 4.2.2 中的一个错误。不完全是我所希望的修复。
I upgraded my local machine to 4.4.2 and that works fine so I guess it is a bug in 4.2.2. Not quite the fix I was hoping for.