0

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]}
user1222302
  • 31
  • 2
  • 7
  • try this: https://stackoverflow.com/a/31995260/156869 – ariefbayu Feb 19 '21 at 04:12
  • Added "curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));" but that made no difference. Can't see anything in the link you provided that is pointing me in a different direction. The post opions I need to send need to be json_rpc and I have them defined in $payload, but my guess is that there is an issue with syntax or something else in $payload, I just can't find what it is. – user1222302 Feb 19 '21 at 05:57
  • Have edited the question with what I belive the issue to be, I think I need to move some double quotation marks in $payload as shown above. – user1222302 Feb 19 '21 at 06:11
  • Would u take a look at my queston related to `json_rpc` plz: https://stackoverflow.com/questions/67605266/how-to-read-smart-contracts-from-tronscan-org-using-laravel-and-json-rpc –  May 22 '21 at 10:13

1 Answers1

0

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!

user1222302
  • 31
  • 2
  • 7