PHP code example of symbol-blockchain-community / symbol-sdk

1. Go to this page and download the library: Download symbol-blockchain-community/symbol-sdk 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/ */

    

symbol-blockchain-community / symbol-sdk example snippets


use SymbolRestClient\Api\TransactionRoutesApi;
use SymbolRestClient\Configuration;

use SymbolSdk\Facade\SymbolFacade;
use SymbolSdk\CryptoTypes\PrivateKey;
use SymbolSdk\Symbol\Models\TransferTransactionV1;
use SymbolSdk\Symbol\Models\UnresolvedMosaic;
use SymbolSdk\Symbol\Models\UnresolvedMosaicId;
use SymbolSdk\Symbol\Models\PublicKey;
use SymbolSdk\Symbol\Models\Amount;
use SymbolSdk\Symbol\Models\NetworkType;
use SymbolSdk\Symbol\Models\Timestamp;

$facade = new SymbolFacade('testnet');
$alice = $facade->createAccount(new PrivateKey('5DB8324E7EB83E7665D500B014283260EF312139034E86DFB7EE736503EA****'));
$bob = $facade->createPublicAccount(new PublicKey('4C4BD7F8E1E1AC61DB817089F9416A7EDC18339F06CDC851495B271533FAD13B'));

$transferTransaction = new TransferTransactionV1(
  network: new NetworkType(NetworkType::TESTNET),
  signerPublicKey: $alice->publicKey,
	deadline: new Timestamp($facade->now()->addHours(2)),
  recipientAddress: $bob->address,
  mosaics: [
    new UnresolvedMosaic(
      mosaicId: new UnresolvedMosaicId('0x72C0212E67A08BCE'),
      amount: new Amount(1)
    )
  ],
  message: "hello, symbol!"
);

$facade->setMaxFee($transferTransaction, 100);
$signature = $alice->signTransaction($transferTransaction);
$payload = $facade->attachSignature($transferTransaction, $signature);

$config = new Configuration();
$config->setHost('https://NODE_URL:3001');
$client = new GuzzleHttp\Client();
$apiInstance = new TransactionRoutesApi($client, $config);

try {
  $result = $apiInstance->announceTransaction($payload);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling TransactionRoutesApi->announceTransaction: ', $e->getMessage(), PHP_EOL;
}

$mosaicId = IdGenerator::generateMosaicId($alice->address);
$mosaicDefinitisonTransaction = new EmbeddedMosaicDefinitionTransactionV1(
  network: new NetworkType(NetworkType::TESTNET),
  signerPublicKey: $alice->publicKey,
  nonce: new MosaicNonce($mosaicId['nonce']),
  id: new MosaicId($mosaicId['id']),
  duration: new BlockDuration(0),
  divisibility: 2,
  flags: new MosaicFlags(MosaicFlags::TRANSFERABLE)
);
$mosaicSupplyChangeTransaction = new EmbeddedMosaicSupplyChangeTransactionV1(
  network: new NetworkType(NetworkType::TESTNET),
  signerPublicKey: $alice->publicKey,
  mosaicId: new UnresolvedMosaicId($mosaicId['id']),
  action: new MosaicSupplyChangeAction(MosaicSupplyChangeAction::INCREASE),
  delta: new Amount(10000)
);
$inner = [$mosaicDefinitisonTransaction, $mosaicSupplyChangeTransaction];
$merkleHash = SymbolFacade::hashEmbeddedTransactions($inner);

$aggTransaction = new AggregateCompleteTransactionV2(
  network: new NetworkType(NetworkType::TESTNET),
  signerPublicKey: $alice->publicKey,
  deadline: new Timestamp($facade->now()->addHours(2)->timestamp),
  transactions: $inner,
  transactionsHash: $merkleHash
);

$facade->setMaxFee($aggTransaction, 100);
$signature = $alice->signTransaction($aggTransaction);
$payload = $facade->attachSignature($aggTransaction, $signature);
try {
  $result = $apiInstance->announceTransaction($payload);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling TransactionRoutesApi->announceTransaction: ', $e->getMessage(), PHP_EOL;
}