Bitcoin Forum
May 21, 2024, 06:52:03 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: createrawtransaction help php  (Read 1028 times)
IceColdTommy (OP)
Full Member
***
Offline Offline

Activity: 168
Merit: 100


View Profile
August 08, 2014, 03:25:17 AM
Last edit: August 08, 2014, 10:10:59 AM by IceColdTommy
 #1

Code:
What am i doing wrong
i have
[code]
$json = json_encode($unspent);
    $funds = $calc - '0.0001';
    $sfunds = ('{\"TTBVKPAqc6wvE3UHHYDDnshxyPEu33Zxjf\":'.$funds.'}');
    $crt = $btc->createrawtransaction("$json,$sfunds");
    
    print_r ($crt);

$json,$sfunds output is

Code:
["{\"txid\":\"9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820\",\"vout\":0}","{\"txid\":\"c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9\",\"vout\":1}"],{\"TTBVKPAqc6wvE3UHHYDDnshxyPEu33Zxjf\":1099.9599}

If i type it directly in console it creates the rawtransaction

But when i try from php i get

Code:
Array ( [.code] => -1 [message] => createrawtransaction [{"txid":txid,"vout":n},...] {address:amount,...} Create a transaction spending given inputs (array of objects containing transaction id and output number), sending to given address(es). Returns hex-encoded raw transaction. Note that the transaction's inputs are not signed, and it is not stored in the wallet or transmitted to the network. )
[/code]
elbandi
Hero Member
*****
Offline Offline

Activity: 525
Merit: 529


View Profile
August 08, 2014, 10:22:04 AM
 #2


["{...}","{..}"]

$unspent variable is wrong format, because json_encode create a string array, not a object array.

use var_dump($unpent); to view the variable data type
IceColdTommy (OP)
Full Member
***
Offline Offline

Activity: 168
Merit: 100


View Profile
August 08, 2014, 10:30:36 AM
 #3


["{...}","{..}"]

$unspent variable is wrong format, because json_encode create a string array, not a object array.

use var_dump($unpent); to view the variable data type
this is the var_dump output
Code:
array (size=2)
  0 => string '{"txid":"9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820","vout":0}' (length=84)
  1 => string '{"txid":"c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9","vout":1}' (length=84)
fbueller
Sr. Member
****
Offline Offline

Activity: 412
Merit: 275


View Profile
August 08, 2014, 01:44:07 PM
 #4

Since you're using a JSON rpc interface, you pass PHP arrays, which get converted to JSON by your library.

$inputs = array(
  array('txid' => '9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820',
     'vout' => 0),
  array('txid' => 'c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9',
     'vout' => 1)
);

$outputs = array('TTBVKPAqc6wvE3UHHYDDnshxyPEu33Zxjf' => 1.01234567);
$raw = $btc->createrawtransaction($inputs, $outputs);

FYI, your JSON before didn't look correct, since you have quotes around the individual inputs? Run json_encode() on each of the above to see what bitcoind will accept if you did it by the CLI.

Bitwasp Developer.
elbandi
Hero Member
*****
Offline Offline

Activity: 525
Merit: 529


View Profile
August 08, 2014, 04:35:49 PM
 #5


["{...}","{..}"]

$unspent variable is wrong format, because json_encode create a string array, not a object array.

use var_dump($unpent); to view the variable data type
this is the var_dump output
Code:
array (size=2)
  0 => string '{"txid":"9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820","vout":0}' (length=84)
  1 => string '{"txid":"c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9","vout":1}' (length=84)
yeah, this is a string array. you can convert it to object, and back to json,

Code:
foreach($unspent as $idx => $output) {
  $unspend[$idx] = json_decode($unspend[$idx]);
}
$json = json_encode($unspent);

if you dont do other things with $unspent, you can just concat it:

Code:
$json = "[" . implode(",", $unpsent) .  "]";
IceColdTommy (OP)
Full Member
***
Offline Offline

Activity: 168
Merit: 100


View Profile
August 08, 2014, 06:15:20 PM
 #6


["{...}","{..}"]

$unspent variable is wrong format, because json_encode create a string array, not a object array.

use var_dump($unpent); to view the variable data type
this is the var_dump output
Code:
array (size=2)
  0 => string '{"txid":"9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820","vout":0}' (length=84)
  1 => string '{"txid":"c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9","vout":1}' (length=84)
yeah, this is a string array. you can convert it to object, and back to json,

Code:
foreach($unspent as $idx => $output) {
  $unspend[$idx] = json_decode($unspend[$idx]);
}
$json = json_encode($unspent);

if you dont do other things with $unspent, you can just concat it:

Code:
$json = "[" . implode(",", $unpsent) .  "]";

i don't do anything else with it but both of the codes, turn $json in string so its not accepted by bitcoind
IceColdTommy (OP)
Full Member
***
Offline Offline

Activity: 168
Merit: 100


View Profile
August 08, 2014, 06:32:04 PM
 #7

Since you're using a JSON rpc interface, you pass PHP arrays, which get converted to JSON by your library.

$inputs = array(
  array('txid' => '9bfc817178ec318f772f678173ae715a2ef661f08c838b5cf00b62d78ede4820',
     'vout' => 0),
  array('txid' => 'c3608c3fc55f6560aa411c9adb3703463e25a0cb2025700aa59c4ab13e245cd9',
     'vout' => 1)
);

$outputs = array('TTBVKPAqc6wvE3UHHYDDnshxyPEu33Zxjf' => 1.01234567);
$raw = $btc->createrawtransaction($inputs, $outputs);

FYI, your JSON before didn't look correct, since you have quotes around the individual inputs? Run json_encode() on each of the above to see what bitcoind will accept if you did it by the CLI.
i passed this directly thru php and it work, thanks
but its unusable in my code i get those inputs from listunspent
IceColdTommy (OP)
Full Member
***
Offline Offline

Activity: 168
Merit: 100


View Profile
August 08, 2014, 07:42:09 PM
 #8

Well it turns out solution was way to easy, so i finally managed to work it out.
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!