Post
Topic
Board Development & Technical Discussion
Re: PHP - Bidcoind check for new transactions.
by
01BTC10
on 09/09/2014, 04:28:37 UTC
I wonder if this snippet could help:

Code:
#!/usr/bin/env php

/* depends on: */
/* php5-curl */
/* https://github.com/aceat64/EasyBitcoin-PHP */

require_once 'include/easybitcoin.php';

$min_confirm 6;
$bitcoinrpc_user 'bitcoinrpc';
$bitcoinrpc_pass 'bitcoinrpc_pass';
$bitcoinrpc_host 'localhost';
$bitcoinrpc_port '8332'

if (
$argc != || strlen($argv[1]) != 64)
  {
    echo 
"usage:$argv[0] tx\n";
    die();
  }

$tx $argv[1];
$file "/tmp/".md5($tx);
$lock fopen($file"w+");

if (
flock($lockLOCK_EX LOCK_NB))
  {
    
fwrite($lock$tx);
  }
else
  {
    echo 
"Process already running for same tx, exiting\n";
    die();
  }

$bitcoin = new Bitcoin($bitcoinrpc_user$bitcoinrpc_pass$bitcoinrpc_host$bitcoinrpc_port);
$bitcoin->gettransaction($tx);

if (
$bitcoin->status != 200)
  {
    echo 
"Error: $bitcoin->error\n";
    die();
  }

if (
$bitcoin->response['result']['details'][0]['category'] === "receive")
  {
    while (
1)
      {
        
$bitcoin->gettransaction($tx);
        
$address =$bitcoin->response['result']['details'][0]['address'];
        
$amount  =$bitcoin->response['result']['amount'];
        
$confirm =$bitcoin->response['result']['confirmations'];

        echo 
"$address -> $amount confirmations:$confirm\n";

        if (
$confirm >= $min_confirm)
          {
            echo 
"$min_confirm or more confirmations for $tx\n";
            
$payment = array("address" => $address"tx" => $tx"amount" => $amount);

            
/****************************************** Process tx stored in $payment here ******************************************/
            
die();
          }

        
sleep(30); // Check number of confirmations every 30sec.
      
}
  }

?>

The infinite loop has to be modified so it won't loop to infinity when getting a tx from a yet to be orphaned block.

Then you only need to add this line to bitcoin.conf to trigger the script for each new tx that is affecting the wallet:
Code:
walletnotify=/path_to/script.php %s