Bitcoin Forum
April 23, 2024, 09:00:35 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [SOLVED] Json-RPC PHP send array  (Read 4654 times)
giantdragon (OP)
Legendary
*
Offline Offline

Activity: 1582
Merit: 1002



View Profile
July 18, 2011, 09:58:35 PM
Last edit: July 18, 2011, 10:32:10 PM by giantdragon
 #1

I want to send multiple payments at once, so I use "sendmany", but seems I wrongly pass arrays. Tried
Code:
for($i = 0; $i < sizeof($list); $i++) {
...
$params[$address] = $amount;
}
$bitcoin->sendmany($params);
and
Code:
$bitcoin->sendmany(array('address1' => 0.001, 'address2' => 0.002));

both doesn't work. What I am doing incorrectly?



Whoever mines the block which ends up containing your transaction will get its fee.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713862835
Hero Member
*
Offline Offline

Posts: 1713862835

View Profile Personal Message (Offline)

Ignore
1713862835
Reply with quote  #2

1713862835
Report to moderator
drawoc
Full Member
***
Offline Offline

Activity: 168
Merit: 100

Firstbits: 175wn


View Profile
July 18, 2011, 10:07:54 PM
 #2

The first parameter is the account name - "" by default.

So, instead of:
Code:
$bitcoin->sendmany($params);
try:
Code:
$bitcoin->sendmany("", $params);

Donate: 175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA
giantdragon (OP)
Legendary
*
Offline Offline

Activity: 1582
Merit: 1002



View Profile
July 18, 2011, 10:21:36 PM
 #3

Code:
$bitcoin->sendmany("", array('address1' => 0.001, 'address2' => 0.002));
Now works. But dynamically created array doesn't works...
Code:
tried
$params[$address] = $amount;
and
$params["$address"] = $amount;
...
$bitcoin->sendmany("", $params);
giantdragon (OP)
Legendary
*
Offline Offline

Activity: 1582
Merit: 1002



View Profile
July 18, 2011, 10:31:56 PM
 #4

Solved. It seems type conversion bug in PHP, just need to explicitly convert amount to float type.
Code:
$params[$address] = doubleval($amount);
drawoc
Full Member
***
Offline Offline

Activity: 168
Merit: 100

Firstbits: 175wn


View Profile
July 18, 2011, 10:32:51 PM
 #5

Glad it's working.

Donate: 175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA
dooglus
Legendary
*
Offline Offline

Activity: 2940
Merit: 1330



View Profile
July 21, 2012, 12:09:01 AM
 #6

this isn't working for me... i've tried every different combination of single quote / double quote but the following just kills the json-rpc connection:

What do you see if you do this:

Quote
$params = array();

var_dump($params);

$params['1addr1'] = 1.2;
$params['1addr2'] = 1.3;

var_dump($params);

var_dump($bitcoin->sendmany('account', $params));

The var_dump should show you what's going wrong.

Maybe it's
  "PHP Fatal error:  Uncaught exception 'Exception' with message 'Request error: {"code":-6,"message":"Account has insufficient funds"}'"
or
  "PHP Fatal error:  Uncaught exception 'Exception' with message 'Request error: {"code":-13,"message":"Error: Please enter the wallet passphrase with walletpassphrase first."}'"
or some such other error.

It should give you an idea of what's going wrong though.

Just-Dice                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   Play or Invest                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   1% House Edge
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 12:59:48 AM
 #7

no, it dies before the 3rd vardump can output anything:

Code:
array(0) {
}

array(2) {
  ["175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA"]=>
  float(0.02)
  ["13QTW27b3SgqrEjefB9PbiZ6hcrVCJAxfx"]=>
  float(0.03)
}

just before that, the json connection is alive and well; i do a getinfo and it works, and sendtoaddress still works.

i am trying this first on an older box, but this [version] => 40101 should be able to handle it, shouldn't it?

also, i don't actually know a 'from' account because i'm using a wallet that's only ever been used in the GUI, so no account names. so I'm leaving that blank:

Code:
var_dump($bitcoin->sendmany('', $params));
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 01:09:37 AM
 #8

i found out how to output what's actually being sent through json:

Code:
Array
(
    [http] => Array
        (
            [method] => POST
            [header] => Content-type: application/json
            [content] => {"method":"sendmany","params":["",{"175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA":0.02,"13QTW27b3SgqrEjefB9PbiZ6hcrVCJAxfx":0.03}],"id":1}
        )

)

can't see anything wrong there though.
dooglus
Legendary
*
Offline Offline

Activity: 2940
Merit: 1330



View Profile
July 21, 2012, 02:06:05 AM
 #9

Do you get any kind of an error message when it quits?  Is there a logfile with errors in maybe?

Where did you get your copy of jsonRPCClient.php from?

What's 40101 the version number of?

Just-Dice                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   Play or Invest                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   1% House Edge
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 02:18:23 AM
 #10

Do you get any kind of an error message when it quits?  Is there a logfile with errors in maybe?

Where did you get your copy of jsonRPCClient.php from?

What's 40101 the version number of?

1. the php log file is just the usual... Warning: fopen(http://...@127.0.0.1:8332/) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in... etc, etc. same as what's printed on the browser with full error reporting.

this was in the tail of bitcoin's debug.log:

Code:

ThreadRPCServer method=getinfo
keypool reserve 1353
keypool return 1353
ThreadRPCServer method=sendmany
askfor tx 2e60cb02f1d7961d518e   0
sending getdata: tx 2e60cb02f1d7961d518e
askfor tx 2e60cb02f1d7961d518e   1342836949000000
askfor tx 2e60cb02f1d7961d518e   1342837069000000
ERROR: ConnectInputs() : 2e60cb02f1 mapTransactions prev not found 9612453f24
ERROR: AcceptToMemoryPool() : ConnectInputs failed 2e60cb02f1
storing orphan tx 2e60cb02f1
askfor tx cc505b8b6973449c51af   0
sending getdata: tx cc505b8b6973449c51af
askfor tx cc505b8b6973449c51af   1342836951000000
AcceptToMemoryPool(): accepted cc505b8b69
askfor tx 0d43330369bb8cf84053   0
sending getdata: tx 0d43330369bb8cf84053
received getdata for: tx cc505b8b6973449c51af
askfor tx 0d43330369bb8cf84053   1342836952000000
askfor tx 3e68f2e0f7bcdede4c9b   0
sending getdata: tx 3e68f2e0f7bcdede4c9b
askfor tx 3e68f2e0f7bcdede4c9b   1342836953000000

are those ERROR's related to the sendmany?

2. i don't remember where my copy came from, but it's worked for every other function i've thrown at it.

3. 40101 is the bitcoin client, as returned by getinfo()

dooglus
Legendary
*
Offline Offline

Activity: 2940
Merit: 1330



View Profile
July 21, 2012, 03:13:08 AM
 #11

I just built a version 4 bitcoind.  I didn't let it touch my wallet, so I don't know if it would work fully, but it gets as far as complaining that the account I'm sending from has insufficient funds.

Can you try code like this and tell me what it shows?

Code:
$params = array();

$params['1addr1'] = 1.2;
$params['1addr2'] = 1.3;

try {
    $bitcoin->sendmany('', $params);
}
catch (Exception $e) {
    print "caught exception: ";
    var_dump($e);
}

Just-Dice                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   Play or Invest                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   1% House Edge
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 03:22:31 AM
 #12

Code:
Array
(
    [http] => Array
        (
            [method] => POST
            [header] => Content-type: application/json
            [content] => {"method":"sendmany","params":["",{"175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA":1.2,"13QTW27b3SgqrEjefB9PbiZ6hcrVCJAxfx":1.3}],"id":1}
        )

)
</pre><br />
<b>Warning</b>:  fopen(http://...@127.0.0.1:8332/) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

 in <b>[full path redacted]\jsonRPCClient.php</b> on line <b>137</b><br />
caught exception: object(Exception)#2 (7) {
  ["message":protected]=>
  string(68) "Unable to connect to http://myusername:mypassword@127.0.0.1:8332/"
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(83) "[full path redacted]\jsonRPCClient.php"
  ["line":protected]=>
  int(145)
  ["trace":"Exception":private]=>
  array(2) {
    [0]=>
    array(6) {
      ["file"]=>
      string(76) "[full path redacted]\index2.php"
      ["line"]=>
      int(16)
      ["function"]=>
      string(6) "__call"
      ["class"]=>
      string(13) "jsonRPCClient"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(2) {
        [0]=>
        string(8) "sendmany"
        [1]=>
        array(2) {
          [0]=>
          string(0) ""
          [1]=>
          array(2) {
            ["175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA"]=>
            float(1.2)
            ["13QTW27b3SgqrEjefB9PbiZ6hcrVCJAxfx"]=>
            float(1.3)
          }
        }
      }
    }
    [1]=>
    array(6) {
      ["file"]=>
      string(76) "[full path redacted]\index2.php"
      ["line"]=>
      int(16)
      ["function"]=>
      string(8) "sendmany"
      ["class"]=>
      string(13) "jsonRPCClient"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(2) {
        [0]=>
        string(0) ""
        [1]=>
        array(2) {
          ["175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA"]=>
          float(1.2)
          ["13QTW27b3SgqrEjefB9PbiZ6hcrVCJAxfx"]=>
          float(1.3)
        }
      }
    }
  }
  ["previous":"Exception":private]=>
  NULL
}

line 137 of the rpc client is this:

Code:
if ($fp = fopen($this->url, 'r', false, $context)) {
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 03:25:55 AM
 #13

oh one other thing (maybe this is where some people will say 'well, duh'), this isn't actually bitcoind. this is bitcoin.exe with server=1 in the config.

this seems to work fine for sendtoaddress... when i call sendtoaddress via json it works and i suddenly see the transaction come up in the GUI.
dooglus
Legendary
*
Offline Offline

Activity: 2940
Merit: 1330



View Profile
July 21, 2012, 05:10:04 AM
 #14

It shouldn't matter whether you use bitcoin -server or bitcoind - they should react the same way to RPC calls.

I'm unable to reproduce having bitcoin fail like it is for you - but also I've not been able to find version 40101.

I only see the following tags in the repository:

$ git tag -l | grep v0.4
v0.4.0
v0.4.00rc1
v0.4.00rc2

and when I build v0.4.0:

$ ./bitcoind getinfo
{
    "version" : 40000,
    [...]

the version string is 40000.

I'm using Linux not Windows.

I'd suggest updating to a recent version of bitcoin and see if that fixes the problem for you.

I'm also wondering what happens if you try running bitcoind from the command line rather than using the PHP interface:

Code:
 $ ./bitcoind sendmany '' '{"1tipznMYcN4SFijpcVXomcynKWAGnNBQ3":1}'
error: {"code":-6,"message":"Account has insufficient funds"}

You may find you need to omit the single quotes around the {...} in Windows - I'm not sure - try it with and without.

Just-Dice                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   Play or Invest                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   1% House Edge
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
July 21, 2012, 05:25:52 AM
 #15

thanks for the help... i'm off this for now, but will reply again if i find out anything else (when i try a different version, etc).
danystatic
Full Member
***
Offline Offline

Activity: 235
Merit: 100


View Profile
October 14, 2013, 04:46:54 PM
 #16

Solved. It seems type conversion bug in PHP, just need to explicitly convert amount to float type.
Code:
$params[$address] = doubleval($amount);

Worked for me, using floatval()
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!