1
我想實現一個簡單的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"}
我可以看到有數據存在,因爲螢火請求頭標籤,我可以看到這樣的:
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://讀取輸入?