<p>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. </p>

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

CC BY-SA 3.0
2016-05-26T14:28:52.750Z
which api service is best?
Patoshi パトシ
CC BY-SA 3.0
2016-05-26T17:47:02.663Z
Any block explorer API should do the job. (i.e. http://dev.blockcypher.com/)
rny
CC BY-SA 3.0
2016-05-26T05:03:24.330Z
2016-05-26T05:03:24.330Z
rny
<p><strong>Cron Jobs and 3rd Party API:</strong> </p>

<p>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. </p>

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

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

<p><strong>3rd Party Webhook:</strong></p>

<p>A better way would be with a webhook. <a href="https://blocktrail.com" rel="nofollow" title="Blocktrail">Blocktrail</a> 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  </p>

<p>Your callback PHP script can look something like this:</p>

<pre><code>$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 &gt;= 1){
//do something, like send an email notification
$email = "your@email.com";
$sub = "New Transaction";
$body = "Amount: ".$amount;
mail($email, $sub, $body);
}
</code></pre>

<p><strong>Bitcoind and Wallet Notify:</strong></p>

<p>Lastly, there's run bitcoind and use walletnotify. </p>

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

<pre><code>walletnotify=curl https://yourwebsite.com/script.php?txid=%s
</code></pre>

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

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

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

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

CC BY-SA 4.0
2018-08-04T00:25:14.413Z
Not sure if Blocktrail still operates as it used to, but I've personally used both https://www.blockchainwebhooks.com/ and https://blockcypher.com for webhooks - both work reliably and have free plans
sizzlecookie
CC BY-SA 3.0
2016-05-27T00:44:29.873Z
2016-08-04T04:58:19.863Z
2016-08-04T04:58:19.863Z
m1xolyd1an
<p>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?</p>

<confirmations><php>
How would one monitor an address for a transaction and 1 confirmation in PHP?
Patoshi パトシ
# How would one monitor an address for a transaction and 1 confirmation in PHP?

(Asked by: username_0 on 2016-05-26)

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?

**Tags:** confirmations, php


---

## 2 answers

### Answer by username_2 on 2016-05-27. Score: 5 | Accepted answer

**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](https://blocktrail.com "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
    }
}

```

> **Comments:**
> **username_3**: Not sure if Blocktrail still operates as it used to, but I've personally used both https://www.blockchainwebhooks.com/ and https://blockcypher.com for webhooks - both work reliably and have free plans  

---

### Answer by username_1 on 2016-05-26. Score: 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.

> **Comments:**
> **username_0**: which api service is best?  
> **username_1**: Any block explorer API should do the job. (i.e. http://dev.blockcypher.com/)  

---

