I'm writing a PHP script to dump Bitcoins from my website's wallet to my other wallet, I put this in a cron job that runs every few minutes to keep my coins pretty safe. The script works if I don't try to send anywhere near the full balance however if I send slightly less it fails and I believe it's due to the fact of TX fees. Below is my current code. I'd like some suggestions as to how to calculate the TX fee if this is causing the problem and how to account for it.
<?
require_once 'jsonRPCClient2.php';
error_reporting(-1);
$bitcoin = new jsonRPCClient('http://coinusr:coinpw:1234/');
$to = "otherwalletaddresshere";
$amount = 0.00;
foreach($bitcoin->listreceivedbyaccount() as $key=>$value){
if($bitcoin->getbalance((string)$value['account']) > "0")
{
$amount = $amount + $value['amount'];
echo($value['account']." has ".$value['amount']."<BR>");
}
}
echo("Sending ".$amount." to ".$to."<br>");
echo($bitcoin->sendtoaddress($to, $amount));
?>