Search code examples
phpbitcoin

Bitcoin sendfrom not returning any errors


I am trying to validate bitcoin engine respond once executing with correct amount within the account balance and correct wallet I am getting transaction ID, but if I enter amount too much and fake wallet I am not receiving any error in return just a blank page with html, head and body elements. Is there is any debug mode or what I can do to receive any response?

$message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));

I am using jsonRPCClient to connect with bitcoin engine.

however when I do that in console using RPC commands

I am getting this : Account has insufficient funds (code -6)

code for redirection

if ($message !== '') {
ob_start(); 
header("Location: mywallet.php?error=done");
die();

} else { 


ob_start(); 
header("Location: mywallet.php?error=true");
die();


    }

Update Yes correct I will more ob_start(); above, thing is that once trying (try,catch) event I am getting blank page upon SUCCESS (so not transaction ID like with normal way I am doing I am getting back transaction ID) upon FAIL I am getting Unable to connect to Bitcoin Server. What I just need sounds very simple, how do I verified that transaction is SUCCESSFUL or FAIL, SUCCESSFUL -> I got ID in return, FAIL -> I have got Error in return. SO I can divert users to right places on the page after form is submitted. Actualy I am doing is withdraw funds form, where user inserts amount and his wallet to get funds back from bitcoin account to his private account. Hope this will helps to understand.

UPDATE 2 I have changed construction for that and seems to is working very well, basically is looking for "Unable" word as transaction ID doesn't have that word and other exception I am getting is "Unable to connect to server..." Thank you for guiding me. Any feedback ?

try {
       $message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
      }
catch (Exception $e) {
       $e->getMessage();

}
// exit;

if (!strpos($e,'Unable') !== false){

header("Location: mywallet.php?error=done");
die();

} else { 


header("Location: mywallet.php?error=true");
die();


    }

Solution

  • What bitcoin php library are you using? Looks like maybe this one?

    If that's the case, it doesn't return an error message, it throws BitCoinClientException

    So you need something like

      try {
           $message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
           }
      catch (Exception $e) {
           echo $e->getMessage();
       }
    

    Updating The ob_start seems superfluous because you don't output anything before the header location. Unless you have output something before you've reached this point in which case you can't send a header. So you'd need to move the ob_start up towards the top of the script before any output.

    Also you aren't sending message to the wallet.php script. Or are you done with it at that point?


    RE: update 2 One thing I might add is the possibility of some other exception message occuring we haven't thought of yet that doesn't contain the "Unable." I'd do something more like:

    $errorOccured = false;
    try {
           $message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
          }
    catch (Exception $e) {
           $errrorMessage = $e->getMessage();
           $errorOccured = true;
    
    }
    
    if (!$errorOccured) {
       ...
     }
    else {
    header("Location: mywallet.php?error=true&errormsg=" . $errorMessage);
    ...
    }
    

    This would require modifying mywallet.php to accept $errorMessage as an additional GET parameter so you can send it back to the user. Might be nice to additionally use another parameter for sending $message on success which would contain the transaction ID.