Bitcoind returns error “JSON value is not an array as expected”, using easybitcoin.php to call sendmany() #13870

issue rhinogroup opened this issue on August 3, 2018
  1. rhinogroup commented at 10:28 PM on August 3, 2018: none

    I am creating a php script to use the sendmany() call to bitcoind. After some database calisthenics involving hitting three tables for permissions, checking user privileges and ensuring the send amounts are correct and there are enough bitcoins on the server, I wind up with the following array to send bitcoins to:

    Array
    (
        [0] => Array
            (
                [coinadd] => mteCLqiEK7v5d3YbDQtxj8oKcdhtHRtXcw
                [amount] => 0.21445033
            )
    
        [1] => Array
            (
                [coinadd] => 2N5aa9FBxGf5xmeLiDz1yJVNYdsfK9GUWWe
                [amount] => 0.02588679
            )
    
        [2] => Array
            (
                [coinadd] => 2Muf4WEzFqNviURTdvkGSswHyrgMzR8optK
                [amount] => 0.02601681
            )
    
    )

    The below foreach generates the array below that.

    foreach($paydata as $pay){
    	$mrules = getmerchrules($pay['merchant_id']);
    	$coinadd = get_merch_address_to_send_to($pay['merchant_id']);
    	if($mrules['autopay'] == 'yes'){
    		if($pay['balance'] > $mrules['minpay']){
    			if($coinadd !== ''){
    				$paynow[$coinadd['coinadd']] = $pay['balance'];
    			}
    		}
    	}
    }
    
    Array
    (
        [mteCLqiEK7v5d3YbDQtxj8oKcdhtHRtXcw] => 0.21445033
        [2N5aa9FBxGf5xmeLiDz1yJVNYdsfK9GUWWe] => 0.02588679
        [2Muf4WEzFqNviURTdvkGSswHyrgMzR8optK] => 0.02601681
    )

    So far so good.

    Now on to the RPC call

    here's the request echoed from the actual easybitcoin curl call. --

    echo "here's the request -- \n".$request."\n"; <-- line 132 easybitcoin.php
    
    {"method":"sendmany","params":["","{\"mteCLqiEK7v5d3YbDQtxj8oKcdhtHRtXcw\":\"0.24020698\",\"2N5aa9FBxGf5xmeLiDz1yJVNYdsfK9GUWWe\":\"0.02588679\",\"2Muf4WEzFqNviURTdvkGSswHyrgMzR8optK\":\"0.02601681\"}",1],"id":4}
    

    EDIT: Some fancy echoing gets me this string which is what is actually sent to the bitcoind server through curl. This string also passes the jsonlint.com test.

    Array
    (
        [19913] => 1
        [52] => 1
        [68] => 10
        [10023] => Array
            (
                [0] => Content-type: application/json
            )
    
        [47] => 1
        [10015] => {"method":"sendmany","params":["","{\"mteCLqiEK7v5d3YbDQtxj8oKcdhtHRtXcw\":\"0.24020698\",\"2N5aa9FBxGf5xmeLiDz1yJVNYdsfK9GUWWe\":\"0.02588679\",\"2Muf4WEzFqNviURTdvkGSswHyrgMzR8optK\":\"0.02601681\"}",1],"id":4}
    )

    EDIT: STILL NO CHANGE

    Array
    (
        [result] => 
        [error] => Array
            (
                [code] => -1
                [message] => JSON value is not an object as expected
            )
    
        [id] => 4
    )

    Aaaand Kablooie! I get the following error from Bitcoind:

    "JSON value is not an array as expected"

    If I turn the JSON string into an object, then the error switches to

    "JSON value is not an object as expected".

    This snippet of code is from the library's __call method in easybitcoin.php

    // If no parameters are passed, this will be an empty array
    $params = array_values($params);
    
    ...
    
    // Build the request, it's ok that params might have any empty array
    $request = json_encode(array(
        'method' => $method,
        'params' => $params,
        'id'     => $this->id
    ));

    Other than slitting my wrists and offering my blood to the programming gods, I've done everything I can think of. Can anyone spot what I am doing wrong here?

    For anyone wondering, this command worked on ./bitcoin-cli

    ./bitcoin-cli sendmany "" "{\"mteCLqiEK7v5d3YbDQtxj8oKcdhtHRtXcw\":0.21445033, \"2N5aa9FBxGf5xmeLiDz1yJVNYdsfK9GUWWe\":0.02588679, \"2Muf4WEzFqNviURTdvkGSswHyrgMzR8optK\":0.02601681}" 1

    Bitcoind response is a txid number

    f4c443881a40054efbd5c3064268a91253f4cb2f7ba8caca96878d0dec46d774

    <!-- This issue tracker is only for technical issues related to Bitcoin Core. General bitcoin questions and/or support requests are best directed to the Bitcoin StackExchange at https://bitcoin.stackexchange.com. For reporting security issues, please read instructions at https://bitcoincore.org/en/contact/. If the node is "stuck" during sync or giving "block checksum mismatch" errors, please ensure your hardware is stable by running memtest and observe CPU temperature with a load-test tool such as linpack before creating an issue! -->

    <!-- Describe the issue -->

    <!--- What behavior did you expect? -->

    <!--- What was the actual behavior (provide screenshots if the issue is GUI-related)? -->

    <!--- How reliably can you reproduce the issue, what are the steps to do so? -->

    <!-- What version of Bitcoin Core are you using, where did you get it (website, self-compiled, etc)? -->

    <!-- What type of machine are you observing the error on (OS/CPU and disk type)? -->

    <!-- Any extra information that might be useful in the debugging process. -->

    <!--- This is normally the contents of a `debug.log` or `config.log` file. Raw text or a link to a pastebin type site are preferred. -->

  2. MarcoFalke commented at 10:31 PM on August 3, 2018: member

    Sorry this is not a PHP help desk.

    Usually the issue tracker is used to track technical issues related to the Bitcoin Core code base. Keep in mind that general bitcoin questions and/or support requests are best directed to the Bitcoin StackExchange.

  3. MarcoFalke closed this on Aug 3, 2018

  4. rhinogroup commented at 10:49 PM on August 3, 2018: none

    Hi Marco, I'm sorry, my concern is if there is something in the bitcoind server itself that may be rejecting the json command. I'm pretty sure that the json is properly formatted. And I am wondering if there is anthing wrong with the format that the bitcoind server returns this error?

    Edit: My apologies for making this look to be about php. Just trying to provide as much info as possible.

  5. MarcoFalke commented at 1:35 AM on August 4, 2018: member

    As you mentioned this works with bitcoin-cli, as well as with python:

    https://github.com/bitcoin/bitcoin/blob/ad552a54c56a420be84b47154882c3e4c76f9bdd/test/functional/wallet_basic.py#L209

    So the issue here is really in your php code or with php in general.

  6. rhinogroup commented at 2:05 AM on August 4, 2018: none

    Aaaand you would be correct. I found the problem. And it was in php. Again, I was not thinking it was in bitcoind, but I was at wits end trying to figure it out. I really appreciate the time you took to answer me. And even though it has nothing to do with you. But I do appreciate it. Thank you.

  7. fanquake added the label Questions and Help on Aug 4, 2018
  8. fanquake locked this on Aug 4, 2018

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-26 06:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me