0

I have Json RPC Server written in Java and I have no code of it.

$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);

In specification server should respond:

{"id":1,"error":{"message":"Internal error","code":-32603},"jsonrpc":"2.0"}

But server is responding nothing and logging

invalid request
com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException: Invalid JSON
    at com.thetransactioncompany.jsonrpc2.JSONRPC2Parser.parseJSONObject(JSO
NRPC2Parser.java:201

Is there a way for debug it or check what I am sending to the server?

kanion
  • 117
  • 1
  • 7

3 Answers3

1

You have to json encode the array first $post = json_encode($post); or CURLOPT_POSTFIELDS, json_encode($post)

EDIT Try This:

<?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);
Mohamed Belal
  • 610
  • 7
  • 11
0

You can use curl_getinfo().

At the end of your script:

print_r(curl_getinfo($ch));
uzyn
  • 6,625
  • 5
  • 22
  • 41
0

You will have to json encode your post data. You are not sending json data right now.

$post = json_encode($post);

and include content length with header

$headers = array(
  'Content-Type: application/json',
  'Content-Length: ' . strlen($post))              
);
Samir Das
  • 1,878
  • 12
  • 20