user1222302
user1222302

Reputation: 31

JSONRPC sendtoaddress returning "result":null,"error", running cURL manually works

I am having trouble finding out why my script is returning "result":null,"error" rather than a successful transaction.

This curl command works when run manually;

curl --user username:password --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "sendtoaddress", "params": ["someaddress", 0.001] }' -H 'content-type: text/plain;' http://".199.42.160.41:8332

Here is the code I'm using attempting to replicate the above curl command which works;

$url = "http://199.42.160.41:8332";
$address = "address"; 
$address = '"'.$address.'"'; //Adding double quotation marks around the address.
$amount = 0.001;
$bounty = $address.', '.$amount;
      
$payload = ["jsonrpc" => "1.0", "id" => "curltest", "method" => "sendtoaddress", "params" => [$bounty] ];
 
$payload=json_encode($payload);

      $ch = curl_init($url); 
      curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);  
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
      curl_setopt($ch, CURLOPT_POSTREDIR, 3);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      $res=curl_exec($ch);
echo "<br/><br/>";
echo "Result from attempting to send transaction is below this line</br>";
print_r($res);
curl_close($ch);

With the above I can see that $payload is set as follows;

$payload is set to the following;

Array ( [jsonrpc] => 1.0 [id] => curltest [method] => sendtoaddress [params] => Array ( [0] => "sendtoaddress", 0.001 ) )

What am I missing or doing wrong?

Edit: My $payload looks like this once it's been converted to jsonrpc

{"jsonrpc":"1.0","id":"curltest","method":"sendtoaddress","params":["someaddress, 0.001"]}

From what I can tell, I need to have it look like this;

{"jsonrpc":"1.0","id":"curltest","method":"sendtoaddress","params":["someaddress", 0.001]}

Upvotes: 0

Views: 285

Answers (1)

user1222302
user1222302

Reputation: 31

Found and fixed the issue. $payload wasn't what I needed, I needed to use two variables for it to be formatted correctly for jsonrpc.

//$payload = ["jsonrpc" => "1.0", "id" => "curltest", "method" => "sendtoaddress", "params" => [$bounty] ];

$payload = ["jsonrpc" => "1.0", "id" => "curltest", "method" => "sendtoaddress", "params" => [$address, $amount] ];

Now that $payload is set correctly the script works!

Upvotes: 0

Related Questions