我一直试图解决这个问题2天。我已经尝试过每一个JSONRPC库,但是没有一个能够工作,所以我自己就这样做了。
我对代理进行简单的AJAX调用 - 用PHP编写 - 并POST请求JSON:
$.ajax({
url: "includes/buoybay_proxy.php",
dataType: "json",
data: {"method": "Test", "params": ["113f8ba8*****************9b5f27e0a0bb"], "id": 1},
timeout: 5000,
type: 'POST',
success: function(data, status, XHR){
console.log("!!: " + JSON.stringify(data));
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("??: ", textStatus, errorThrown);
}
});
代理 - 用PHP编写 - 工作原理如下:
$con = json_encode($_POST);
$cparams = array(
'http' => array(
'content' => $con,
'method' => 'POST',
'ignore_errors' => true,
'header' => 'POST /studs/**************/server.php HTTP/1.0\r\n
User-Agent: "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36\r\n"
Host: my.wamp.server:80\r\n
Accept-Charset: UTF-8,ISO-8859-1,US-ASCII\r\n
Content-Type: application/json\r\n
Content-Length: ' . strlen($con) . '\r\n'
)
);
//ChromePhp::log($cparams);
$context = stream_context_create($cparams);
$fp = fopen('http://mw.buoybay.org:80/studs/**************/server.php', 'r', false, $context);
//ChromePhp::log(stream_get_contents(($fp)));
echo json_encode(stream_get_contents(($fp)));
这会导致来自服务器的以下错误消息:
{"id": null, "error": { "faultCode": 15, "faultString": "Invalid request payload Invalid data (empty string?)" }, "result": null}
我只是出于想法;谁能解决这个问题,或者至少指出我正确的方向?
答案 0 :(得分:0)
这就是最终的工作(PHP不是我写的); AJAX很好,除了我应该使用GET。
AJAX代理:
<?php
require_once('jsonRPCClient.php');
include('ChromePhp.php');
$url='http://mw.buoybay.org/studs/studs_cdrh/jsonrpc_cdrh/server.php';
$constellation="CBIBS";
$apikey="113f8ba8f286ad********************f27e0a0bb";
$client =new jsonRPCClient($url,true);
$method=$_GET['method'];//"RetrieveCurrentReadings";
$getParams = $_GET['params'];
$params=explode(',', $getParams); //array('CBIBS','AN','113f8ba8f28******************279b5f27e0a0bb');
$result = $client->__call($method, $params);
// return the results
echo json_encode($result);
?>