PHP code example of cryptothree / solana-php-sdk

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

    

cryptothree / solana-php-sdk example snippets


use Attestto\SolanaPhpSdk\Connection;
use Attestto\SolanaPhpSdk\SolanaRpcClient;

// Using a defined method
$sdk = new Connection(new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT));
$accountInfo = $sdk->getAccountInfo('4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA');
var_dump($accountInfo);

use Attestto\SolanaPhpSdk\SolanaRpcClient;

$client = new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT);
$accountInfoResponse = $client->call('getAccountInfo', ['4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA']);
$accountInfoBody = $accountInfoResponse->json();
$accountInfoStatusCode = $accountInfoResponse->getStatusCode();

$client = new SolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT);
$connection = new Connection($client);
$fromPublicKey = KeyPair::fromSecretKey([...]);
$toPublicKey = new PublicKey('J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99');
$instruction = SystemProgram::transfer(
    $fromPublicKey->getPublicKey(),
    $toPublicKey,
    6
);

$transaction = new Transaction(null, null, $fromPublicKey->getPublicKey());
$transaction->add($instruction);

$txHash = $connection->sendTransaction($transaction, $fromPublicKey);

class DidData
{

    use BorshObject; //trait
    public $keyData;

    public const SCHEMA = [
        VerificationMethodStruct::class => VerificationMethodStruct::SCHEMA[VerificationMethodStruct::class],
        ServiceStruct::class => ServiceStruct::SCHEMA[ServiceStruct::class],
        self::class => [
            'kind' => 'struct',
            'fields' => [
                ['offset', 'u64'],
                ['version', 'u8'],
                ['bump', 'u8'],
                ['nonce', 'u64'],
                ['initialVerificationMethod', 'string'],
                ['flags', 'u16'],
                ['methodType', 'u8'],
                ['keyData', ['u8']],
                ['verificationMethods', [VerificationMethodStruct::class]],
                ['services', [ServiceStruct::class]],
                ['nativeControllers', ['pubKey']],
                ['otherControllers', ['string']],
            ],
        ],
    ];

    public static function fromBuffer(array $buffer): self
    {
        return Borsh::deserialize(self::SCHEMA, self::class, $buffer);
    }
}

/**
     * deserializeDidData
     *
     * @param string $dataBase64 The base64 encoded data of the DID data account
     * @return DidData The deserialized DID data object
     * @example DidSolProgram::deserializeDidData('TVjvjfsd7fMA/gAAAA...');
     */
    static function deserializeDidData($dataBase64)
    {

        $base64String = base64_decode($dataBase64);
        $uint8Array = array_values(unpack('C*', $base64String));
        $didData = DidData::fromBuffer($uint8Array); // See above code block

        $keyData = $didData->keyData;

        $binaryString = pack('C*', ...$keyData);

        $b58 = new Base58();
        $base58String = $b58->encode($binaryString);
        $didData->keyData = $base58String;
        return $didData;
    }
bash
composer 
bash
git clone https://github.com/Attestto-com/solana-php-sdk.git

cd solana-php-sdk

composer install 

bash
docker build -t solana-php-sdk .
bash
docker run -it solana-php-sdk /bin/bash

OR