익명 10:48

How would one monitor an address for a transaction and 1 confirmation in PHP?

How would one monitor an address for a transaction and 1 confirmation in PHP?

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?



Top Answer/Comment:

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 = "[email protected]";
$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
    }
}
상단 광고의 [X] 버튼을 누르면 내용이 보입니다