PHP 与 JsonRPCServer 的 Curl 连接无效 Json
PHP Curl connection to JsonRPCServer Invalid Json
我用 Java 编写了 Json RPC 服务器,但我没有它的代码。
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
$url="https://127.0.0.1:9999";
$pemfile = "C:\openssl\client.cer";
$keyfile = "C:\openssl\client.openssl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$post = array(
"jsonrpc" => "2.0",
"method" => "generateAddress",
"id" => "1");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_SSLCERT, $pemfile);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, '');
curl_setopt($ch, CURLOPT_SSLKEY, $keyfile);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, '');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
print_r($data);
在规范中服务器应该响应:
{"id":1,"error":{"message":"Internal error","code":-32603},"jsonrpc":"2.0"}
但是服务器没有任何响应并且正在记录
invalid request
com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException: Invalid JSON
at com.thetransactioncompany.jsonrpc2.JSONRPC2Parser.parseJSONObject(JSO
NRPC2Parser.java:201
有没有办法调试它或检查我发送到服务器的内容?
您可以使用 curl_getinfo()
。
在你的脚本结尾处:
print_r(curl_getinfo($ch));
您必须先 json 对数组进行编码 $post = json_encode($post);
或 CURLOPT_POSTFIELDS, json_encode($post)
编辑试试这个:
<?php
$headers = array(
'Content-Type: application/json'
);
$url="https://127.0.0.1:9999";
$post = array(
"jsonrpc" => "2.0",
"method" => "generateAddress",
"id" => "1"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$data = curl_exec($ch);
print_r($data);
您将必须 json 编码您的 post 数据。您现在没有发送 json 数据。
$post = json_encode($post);
并使用 header
包含内容长度
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post))
);
我用 Java 编写了 Json RPC 服务器,但我没有它的代码。
$headers = array(
'Content-Type: application/json',
'Accept: application/json'
);
$url="https://127.0.0.1:9999";
$pemfile = "C:\openssl\client.cer";
$keyfile = "C:\openssl\client.openssl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$post = array(
"jsonrpc" => "2.0",
"method" => "generateAddress",
"id" => "1");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_SSLCERT, $pemfile);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, '');
curl_setopt($ch, CURLOPT_SSLKEY, $keyfile);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, '');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
print_r($data);
在规范中服务器应该响应:
{"id":1,"error":{"message":"Internal error","code":-32603},"jsonrpc":"2.0"}
但是服务器没有任何响应并且正在记录
invalid request
com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException: Invalid JSON
at com.thetransactioncompany.jsonrpc2.JSONRPC2Parser.parseJSONObject(JSO
NRPC2Parser.java:201
有没有办法调试它或检查我发送到服务器的内容?
您可以使用 curl_getinfo()
。
在你的脚本结尾处:
print_r(curl_getinfo($ch));
您必须先 json 对数组进行编码 $post = json_encode($post);
或 CURLOPT_POSTFIELDS, json_encode($post)
编辑试试这个:
<?php
$headers = array(
'Content-Type: application/json'
);
$url="https://127.0.0.1:9999";
$post = array(
"jsonrpc" => "2.0",
"method" => "generateAddress",
"id" => "1"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$data = curl_exec($ch);
print_r($data);
您将必须 json 编码您的 post 数据。您现在没有发送 json 数据。
$post = json_encode($post);
并使用 header
包含内容长度$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post))
);