Question

I am making a bitcoin faucet using the coinbase api, and was looking to validate the address. I looked online to see if there are any good scripts, and couldnt find any so I decided to test and see if it was already built in the API, and it was! The the thing is that instead of just saying that is not a valid address it php displays a LONG error...

Fatal error: Uncaught exception 'Coinbase_ApiException' with message 'Please enter a valid email or bitcoin address' in C:\xampp\htdocs\nahtnam\lib\Coinbase\Rpc.php:84 Stack trace: #0 C:\xampp\htdocs\nahtnam\lib\Coinbase\Coinbase.php(39): Coinbase_Rpc->request('POST', 'transactions/se...', Array) #1 C:\xampp\htdocs\nahtnam\lib\Coinbase\Coinbase.php(118): Coinbase->post('transactions/se...', Array) #2 C:\xampp\htdocs\nahtnam\faucet.php(54): Coinbase->sendMoney('17FSKMPAyXGR7EQ...', '0.00000555', 'this is a test') #3 {main} thrown in C:\xampp\htdocs\nahtnam\lib\Coinbase\Rpc.php on line 84

Is ther any way i can just set $address_error to "Please enter a valid Address" (not email) if this occurs and also not display the error? Thanks!

Was it helpful?

Solution

Expanding on aliasm2k's answer, you probably want to do it more like this:

EDIT: Changed answer slightly based on comments discussion

I think I was a bit unclear on what you were asking for in the comments.

 try {
     $result = $Coinbase->sendMoney($bitcoinaddress, '0.00000555', 'this is a test');
 catch(Exception $e) {
     echo $e->getMessage(); 
     exit;   //optional but you probably want to quit here and show the user
             //the original form along with the error message to fix 
 }

This is just going to echo "Please enter a valid email or bitcoin address." You wont get all of that other info because you are catching the exception and just displaying the message. Possible error messages are listed here.

Also, if I can give you a slightly off-topic hint: If you want to find info about a particular address that has been used, try the blockchain block explorer api.

And to simply check if an address is valid, you need to calculate that in your code or find a library function to do so. There's no master-list of addresses that an api would have. The last 4 bytes of the address are a double-sha-256 checksum of the preceding characters. That's sort-of an imprecise description I'm giving you, by the way, but check here for a working php example

OTHER TIPS

Use try and catch.

try {
    if(/*invalid address check returns true*/)
        throw 'Invalid address';
} catch(Exception $e) {
    echo 'Exception: ' . $e->getMessage();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top