<p>Your callback will update your server but the user would have to refresh the page to see the change, or you could use AJAX (but that can be wasteful as it will send several requests until there's an update). I'd suggest you use their websocket API to monitor for a transaction so that you can notify the user live on the page. </p>

<p>Below is a simple example, just be sure you update the <code>address</code> variable with the address you want to monitor. You can put your PHP output in the javascript like     <code>var address = &lt;?php echo $payTo; ?&gt;;</code></p>

<pre><code>&lt;html&gt;
User Page&lt;br&gt;
&lt;div id="notifications"&gt;Waiting for Payment...&lt;/div&gt;
&lt;script&gt;
var address = "BTC_ADDRESS_TO_MONITOR";
var btcs = new WebSocket('wss://ws.blockchain.info/inv');
btcs.onopen = function(){
    btcs.send(JSON.stringify({"op":"addr_sub", "addr":address}));
};
btcs.onmessage = function(onmsg)
{
  var response = JSON.parse(onmsg.data);
  var getOuts = response.x.out;
  var countOuts = getOuts.length; 
  for(i = 0; i &lt; countOuts; i++)
  {
    //check every output to see if it matches specified address
    var outAdd = response.x.out[i].addr;
    var specAdd = address;
       if (outAdd == specAdd)
       {
       var amount = response.x.out[i].value;
       var calAmount = amount / 100000000;
       document.getElementById("notifications").innerHTML = "Received: " + calAmount + "BTC";
       };
  }; 
}
&lt;/script&gt;
&lt;/html&gt;
</code></pre>

CC BY-SA 3.0
2017-01-09T18:56:13.223Z
2017-01-09T18:56:13.223Z
m1xolyd1an
<p>I want to start accepting Bitcoins payment on my site, I got the callback and the index working fine, the only thing is that it won't update the index once the payment is received, I'd like to give the user some kind of live feedback when the payment is received, how can I do that?</p>

<p>Below is what I got.</p>

<blockquote>
  <p>Index.php</p>
</blockquote>

<pre><code>&lt;?php

$api_key = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx";

$xpub = "xpubxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$secret = "xxxxxxxxxxxxxxxxxxxx";

$rootURL = "https://xxxxxxxxxx.org/BlockChain";

$orderID = uniqid();

$callback_url = $rootURL . "/callback.php?invoice=" . $orderID . "&amp;secret=" . $secret . "&amp;username=Teste" . "&amp;password=qwerty123";
$receive_url = "https://api.blockchain.info/v2/receive?key=" . $api_key . "&amp;xpub=" . $xpub . "&amp;callback=" . urlencode($callback_url);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $receive_url);
$ccc = curl_exec($ch);

$json = json_decode($ccc, true);

$payTo = $json['address'];

 echo $payTo;

?&gt;
</code></pre>

<p>Callback.php
    

<pre><code>    $secret = "xxxxxxxxxxxxxxxxxxxx";

    if ($_GET['secret'] != $secret) {
        die("Stop doing that!");
    }

    else {
        //Register new user, insert more days, etc...

        //# START DEBUG #
        $fff = fopen("text.txt", "w");
        $value = $_GET['value'] . " - ";
        $fw = fwrite($fff, $value);

        $txhash = $_GET['transaction_hash'] . " - ";
        $fw = fwrite($fff, $txhash);

        $invoice = $_GET['invoice'] . " - ";
        $fw = fwrite($fff, $invoice);

        $value_in_btc = $_GET['value'] / 100000000 . " - ";
        $fw = fwrite($fff, $value_in_btc);

        $username = $_GET['username'] . " - ";
        $fw = fwrite($fff, $username);

        $password = $_GET['password'] . " - ";
        $fw = fwrite($fff, $password);

        fclose($fff);
        //# END DEBUG #

        echo "*ok*"; //Tell blockchain everything is ok, so they stop.
    }

    ?&gt;
</code></pre>

<blockchain><api><php>
How to update index when payment is received with Blockchain PHP API?
Vinny
# How to update index when payment is received with Blockchain PHP API?

(Asked by: username_0 on 2017-01-09)

I want to start accepting Bitcoins payment on my site, I got the callback and the index working fine, the only thing is that it won't update the index once the payment is received, I'd like to give the user some kind of live feedback when the payment is received, how can I do that?

Below is what I got.

> Index.php

```
<?php

$api_key = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx";

$xpub = "xpubxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$secret = "xxxxxxxxxxxxxxxxxxxx";

$rootURL = "https://xxxxxxxxxx.org/BlockChain";

$orderID = uniqid();

$callback_url = $rootURL . "/callback.php?invoice=" . $orderID . "&secret=" . $secret . "&username=Teste" . "&password=qwerty123";
$receive_url = "https://api.blockchain.info/v2/receive?key=" . $api_key . "&xpub=" . $xpub . "&callback=" . urlencode($callback_url);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $receive_url);
$ccc = curl_exec($ch);

$json = json_decode($ccc, true);

$payTo = $json['address'];

 echo $payTo;

?>

```

Callback.php

```
    $secret = "xxxxxxxxxxxxxxxxxxxx";

    if ($_GET['secret'] != $secret) {
        die("Stop doing that!");
    }

    else {
        //Register new user, insert more days, etc...

        //# START DEBUG #
        $fff = fopen("text.txt", "w");
        $value = $_GET['value'] . " - ";
        $fw = fwrite($fff, $value);

        $txhash = $_GET['transaction_hash'] . " - ";
        $fw = fwrite($fff, $txhash);

        $invoice = $_GET['invoice'] . " - ";
        $fw = fwrite($fff, $invoice);

        $value_in_btc = $_GET['value'] / 100000000 . " - ";
        $fw = fwrite($fff, $value_in_btc);

        $username = $_GET['username'] . " - ";
        $fw = fwrite($fff, $username);

        $password = $_GET['password'] . " - ";
        $fw = fwrite($fff, $password);

        fclose($fff);
        //# END DEBUG #

        echo "*ok*"; //Tell blockchain everything is ok, so they stop.
    }

    ?>

```

**Tags:** blockchain, api, php


---

## 1 answers

### Answer by username_1 on 2017-01-09. Score: 1 | Accepted answer

Your callback will update your server but the user would have to refresh the page to see the change, or you could use AJAX (but that can be wasteful as it will send several requests until there's an update). I'd suggest you use their websocket API to monitor for a transaction so that you can notify the user live on the page.

Below is a simple example, just be sure you update the `address` variable with the address you want to monitor. You can put your PHP output in the javascript like `var address = <?php echo $payTo; ?>;`

```
<html>
User Page<br>
<div id="notifications">Waiting for Payment...</div>
<script>
var address = "BTC_ADDRESS_TO_MONITOR";
var btcs = new WebSocket('wss://ws.blockchain.info/inv');
btcs.onopen = function(){
    btcs.send(JSON.stringify({"op":"addr_sub", "addr":address}));
};
btcs.onmessage = function(onmsg)
{
  var response = JSON.parse(onmsg.data);
  var getOuts = response.x.out;
  var countOuts = getOuts.length; 
  for(i = 0; i < countOuts; i++)
  {
    //check every output to see if it matches specified address
    var outAdd = response.x.out[i].addr;
    var specAdd = address;
       if (outAdd == specAdd)
       {
       var amount = response.x.out[i].value;
       var calAmount = amount / 100000000;
       document.getElementById("notifications").innerHTML = "Received: " + calAmount + "BTC";
       };
  }; 
}
</script>
</html>

```


---

