Skip to content
Process Ethereum Blockchain data with PHP or continuously react on latest Blocks or contract Events
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.

Ethereum-php Listener and Indexer


When developing dapps you might need some Backend process to react data of the latest block or on on-chain Events (Solidity Events). You might fill up some database by indexing all blocks or set up a daemon process to do something every time a new Block is created.

As much as we want real decentralization, the reality is actually different. Mots Dapp's require a Backend process. Sometimes for additional data which is too expensive to store (semi decentralized apps) or at least for monitoring what is happening at on chain.

You can do this in PHP very easily. E.g Indexing a chain from block 0 to the latest at script start time:

$web3 = new Ethereum('');
// Block 0 -> last block.
new BlockProcessor($web3, function (Block $block) {

    // This will be run on every Block.
    print "\n\n#### BLOCK NUMBER " . $block->number->val() . " ####\n";

    // Add to database... 

Run the script. E.g:

php app/basic-blockProcessor-example.php

You need to run composer install in the app directory to load the dependencies.

Integration with Truffle and Contract Events

If you are using Truffle to develop you Dapp you can easily set up a monitoring system for your smart contracts:

// Extend a \Ethereum\SmartContract with EventHandlers
class CallableEvents extends SmartContract {
  public function onCalledTrigger1 (EthEvent $event) {
    echo '### ' . substr(__FUNCTION__, 2) . "(\Ethereum\EmittedEvent)\n";
  public function onCalledTrigger2 (EthEvent $event) {
    echo '### ' . substr(__FUNCTION__, 2) . "(\Ethereum\EmittedEvent)\n";

$web3 = new Ethereum('');
$networkId = '5777';

// Contract Classes must have same name as the solidity classes for this to work.
$contracts = SmartContract::createFromTruffleBuildDirectory(

// process any Transaction from current Block to the future.
new ContractEventProcessor(


The Loop script is based on reactphp which is actually older that Javascript React.

The Ethereum part is based on Ethereum-php library.

You might use ganache-cli-docker-compose for testing.

You may use the indexer with Infura Ethereum as a Service, as it doesn't rely on filters, which Infura does not support.

I used a very simple DAPP to interact with the CallableEvents smart contract used as a example here.


# Build
docker build -t ethereum-php-event-handler .

# Login
docker run -v $(pwd)/app:/opt/local/php-ethereum-listener -it ethereum-php-event-handler bash

# Deamon 
docker run -v $(pwd)/app:/opt/local/php-ethereum-listener -it ethereum-php-event-handler bash -c '/opt/local/bin/'
You can’t perform that action at this time.