7

I have a small PHP script with my static bitcoin address shown. When someone sends any btc to that address, how can I get the PHP script to check if the transaction has had 1 confirmation? Do I have to run something on cron to have it check every second? Or is there another elegant way to do this in PHP? Any examples?

4

Cron Jobs and 3rd Party API:

You could try with cron jobs, but I wouldn't run it every second, that's rather exhaustive. Maybe every 5 or 10 minutes, and then have it check the current total received vs the previous amount using a database.

$addy = "1somebitcoinaddress";
$bcinfo = json_decode(file_get_contents("https://blockchain.info/address/".$addy."?format=json"), true);
$balance = $bcinfo["total_received"];
$query = $db->prepare("SELECT previous_balance FROM address WHERE address = :addy");
$query->bindParam(':addy', $addy);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$prevBal = $result["previous_balance"];
   if($balance > $prevBal){
   //balance changed, do something
   }

That way seems rather less than elegant though, and it's not instant.

3rd Party Webhook:

A better way would be with a webhook. Blocktrail offers a free webhook service, and you can even do it all in their UI after creating an account. Just sign up as a developer, then once logged in click on the webhooks tab and then create a new webhook that monitors your bitcoin address. It will ask for a callback url, that's where you provide a link to your PHP script to run. When you provide the callback url I would add a secret to help prevent unauthorized calls like: yourdomain.com/callback.php?secret=12345

Your callback PHP script can look something like this:

$secret = "12345";
$verify = $_GET["secret"];
if($secret != $verify){
die();
}

$data = json_decode(file_get_contents("php://input"), true);

//get estimated value of transaction
$amount = $data["data"]["estimated_value"];
$confirm = $data["data"]["confirmations"];

if($confirm >= 1){
//do something, like send an email notification
$email = "your@email.com";
$sub = "New Transaction";
$body = "Amount: ".$amount;
mail($email, $sub, $body);
}

Bitcoind and Wallet Notify:

Lastly, there's run bitcoind and use walletnotify.

Make sure the address you are monitoring is imported into your bitcoind wallet. Set wallet notify up in your bitcoin.conf

walletnotify=curl https://yourwebsite.com/script.php?txid=%s

Now anytime there is activity on your bitcoind wallet your script will run, with the transaction id stored in a GET under ["txid"] and then you can do a script like:

require("easybitcoin.php");
$bitcoin = new Bitcoin("someusername", "somepassword");

$txid = $_GET["tx"];
$txinfo = $bitcoin->gettransaction($txid);
$details = count($txinfo["details"]);

for($i=0;$i<$details;$i++){
$check = $txinfo["details"][$i]["address"];
$addy = "1SomeAddressToMonitor";
    if($check == $addy){
    //activity on your address, do something
    }
}
1

Depending on your trust model there are various ways to do this. Firstly you may consider subscribing to a well known API service that notifies you of a transaction confirmation. That requires trust in a third party. If that is not suitable then you'll have to run a full node or spv node connected to the Bitcoin network and implement the interface to said node in your script.

Finally you can run this script of yours in background to prompt you of the transaction confirmation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.