1. Go to this page and download the library: Download dev404ai/drand-client-php 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/ */
dev404ai / drand-client-php example snippets
use Drand\Client\DrandClient;
// Create a client with default settings
$client = DrandClient::createDefault();
// Get the latest beacon (scheme is detected and verified automatically)
$beacon = $client->getLatestBeacon();
printf("Round %d: %s\n", $beacon->getRound(), $beacon->getRandomness());
// Get a specific round
$beacon = $client->getBeacon(12345);
// Get chain information
$chain = $client->getChain();
printf("Chain period: %d seconds\n", $chain->getPeriod());
printf("Signature scheme: %s\n", $chain->getSchemeID());
// Get the round number for a specific time
$roundNumber = $client->roundAt(time() + 3600); // Round an hour from now
// Get the time when a round will be available
$roundTime = $client->roundTime(12345);
$whenAvailable = new DateTime("@$roundTime");
// Calculate current round
$currentRound = $client->roundAt();
use Drand\Client\HttpClient;
use Drand\Client\CachingHttpClient;
// Create HTTP client with caching
$httpClient = new HttpClient('https://api.drand.sh');
$cachingClient = new CachingHttpClient(
$httpClient,
300, // Chain info TTL (5 minutes)
30 // Beacon TTL (30 seconds)
);
// Clear cache if needed
$cachingClient->clearCache();
use Drand\Client\HttpClient;
use Drand\Client\MultiChainClient;
// Create clients for different drand networks
$mainnet = new HttpClient('https://api.drand.sh');
$testnet = new HttpClient('https://pl-us.testnet.drand.sh');
// Combine them into a MultiChainClient
$multi = new MultiChainClient([$mainnet, $testnet]);
// Select by chain hash or beaconID
$mainnetClient = $multi->forChainHash('8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce');
$testnetClient = $multi->forBeaconID('default');
// Use as a regular client (default is the first one)
$beacon = $multi->getLatestBeacon();
use Drand\Client\HttpClient;
use Drand\Client\FastestNodeClient;
// Create clients for several public endpoints
$clients = [
new HttpClient('https://api.drand.sh'),
new HttpClient('https://drand.cloudflare.com'),
new HttpClient('https://drand.cloudflare.com/public')
];
// FastestNodeClient will always use the fastest node (auto-updated)
$fastest = new FastestNodeClient($clients);
$beacon = $fastest->getLatestBeacon();
printf("Fastest node randomness: %s\n", $beacon['randomness']);