PHP code example of utopia-php / replication

1. Go to this page and download the library: Download utopia-php/replication library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

utopia-php / replication example snippets


use Utopia\Replication\Source\MySQL;

// Source is the polymorphic interface; MySQL is the binlog implementation.
$replication = new MySQL(
    host: '127.0.0.1',
    port: 3306,
    username: 'replicator',
    password: 'secret',
    serverId: 1001,            // unique among replicas
    schema: 'appwrite',        // only emit changes for this database
    ssl: false,                // when true, TLS verifies the server cert by default
);

$replication->start($checkpoint ?? null);  // resume from a GTID set, or null for "now"

foreach ($replication->getChanges() as $change) {
    // $change->action  — Change::INSERT | UPDATE | DELETE
    // $change->table   — physical table name
    // $change->rows    — list of column => value maps (after-image for updates)
    // $change->gtid    — executed-GTID-set checkpoint (advance on commit)

    foreach ($change->rows as $row) {
        // react to the change ...
    }

    $checkpoint = $change->gtid;     // persist to resume after a restart
}

use Utopia\Replication\Source\MySQL\Decoder;
use Utopia\Replication\Source\MySQL\EventParser;
use Utopia\Replication\Source\MySQL\File;
use Utopia\Replication\Source\MySQL\GtidSet;

$source = new File($bytes);   // a string, or an iterable of byte chunks
$source->open();

// Offline there is no server to resolve column names from, so the binlog must
// carry them (binlog_row_metadata=FULL) or you pass EventParser a resolver.
$decoder = new Decoder(new EventParser(), new GtidSet(), 'appwrite', $source->checksum());

foreach ($source->events() as $event) {
    $change = $decoder->decode($event);
    if ($change !== null) {
        // react to the change ...
    }
}
bash
composer