Yes, I'm a newbie but I'm a bitcoin expert
Solid effort. But I suspect that the reason the OP specified that his sendTX() function takes a private key is that he doesn't have access to a Bitcoin Core node to be able to make use of RPC. I believe this is also why he mentioned using APIs at blockcypher/sochain etc to retrieve UTXOs.
And one small piece of advice... next time, when posting code, please use [ code ][ /code ] formatting tags (without the spaces)... it makes code a LOT easier to read when it is indented properly in monospaced font, and it also prevents your posts from getting excessively long
require("jsonRPCClient.php");
$bitcoin = new jsonRPCClient('http://username:[email protected]:port');
$addresses = array (
"address1" => "address", //fill in address you want to spend from
"address2 => "address" //fill in address you want to spend from
);
$unspentTransactions = $bitcoin->listunspent(0, 99999, $addresses);
$collectedTransactions = [];
foreach ($unspentTransactions as $unspentTransaction) {
if ($collectedAmount < $amountToSend) {
$collectedTransactions[] = [
'txid' => $unspentTransaction['txid'],
'vout' => $unspentTransaction['vout'],
];
$collectedAmount += $unspentTransaction['amount']; //this shows all balance collected from addesses you wish to spend from
} else {
break;
}
}
$recipient = "address"; //this where you want to send payment to
$amount = 0.000000; //amount you wish to send in BTC value
$change_address = "your_change_Address";
$change_amount = number_format($collectedAmount-$fee-$amount, 8);
$output =
array(
"".$recipient."" =>"".$amount."",
//this is where the change money will go back to... set your address
"".$change_addr."" =>"".$change_amount-$fee."",
);
// Now let's create the transaction
$rawTransaction = $bitcoin->createrawtransaction($collectedTransactions, $output);
// now lets sign the transaction using bitcoin core signwithwallet function
$sign_raw_txn = $bitcoin->signrawtransactionwithwallet($rawTransaction);
$shex = $sign_raw_txn['hex']; // this is the signed hex you can do anything you want with it... if you want to send payment follow the next step.
echo $bitcoin->sendrawtransaction($shex); // this will return txn id and you're all done.
//I haven't tested this code so if you want me to code it for you I can do in my spare time, I hope this helps.
ps. There is also a missing " in your "address2 => "address" line...