PHP code example of aternos / etcd

1. Go to this page and download the library: Download aternos/etcd 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/ */

    

aternos / etcd example snippets




$client = new Aternos\Etcd\Client();
$client = new Aternos\Etcd\Client("localhost:2379");
$client = new Aternos\Etcd\Client("localhost:2379", "username", "password");

// currently implemented functions
$client->put("key", "value");
$client->get("key");
$client->delete("key");
$client->putIf("key", "newValue", "valueToCompareWith");
$client->deleteIf("key", "valueToCompareWith");

// complex transaction example
$leaseId = $client->getLeaseID(10);
$putOp = $client->getPutOperation('key', 'someValueToPutOnSuccess', $leaseId);
$getOp = $client->getGetOperation('key');
// following compare checks for key existence
$compare = $client->getCompare('key', '0', \Etcdserverpb\Compare\CompareResult::EQUAL, \Etcdserverpb\Compare\CompareTarget::MOD);
// execute Put operation and return the key we stored, just return the key value if it already exists
$txnResponse = $client->txnRequest([$putOp, $getOp], [$getOp], [$compare]);
$result = $client->getResponses($txnResponse, 'response_range', true);
// $result[0] contains "someValueToPutOnSuccess"



$clients = [
    new Aternos\Etcd\Client("hostA:2379"),
    new Aternos\Etcd\Client("hostB:2379"),
    new Aternos\Etcd\Client("hostC:2379")
];
$shardedClient = new Aternos\Etcd\ShardedClient($clients);

$shardedClient->put("key", "value");
$shardedClient->get("key");



$clients = [
    new Aternos\Etcd\Client("hostA:2379"),
    new Aternos\Etcd\Client("hostB:2379"),
    new Aternos\Etcd\Client("hostC:2379")
];
$failoverClient = new Aternos\Etcd\FailoverClient($clients);

// set 60 seconds as a hold-off period between another connection attempt to the failing host 
// default is 120 seconds
// failing host is being remembered within FailoverClient object instance 
$failoverClient->setHoldoffTime(60);
$failoverClient->put("key", "value");
$failoverClient->get("key");
bash
sudo apt install php php-dev php-pear
sudo pecl install grpc