The PHP SDK allows developers to interact with the Casper Network using PHP. This page covers different examples of using the SDK.
composer require make-software/casper-php-sdk
For using Secp256K1
keys you need to install and enable secp256k1-php extension:
git clone git@github.com:bitcoin-core/secp256k1 && \
cd secp256k1 && \
./autogen.sh && \
./configure --enable-experimental --enable-module-{ecdh,recovery} && \
make && sudo make install && \
cd ../
git clone git@github.com:Bit-Wasp/secp256k1-php && \
cd secp256k1-php/secp256k1 && \
phpize && \
./configure --with-secp256k1 && \
make && sudo make install && \
cd ../../
Enable extension by adding the following line to your php.ini
file
extension=secp256k1.so
Create RpcClient
by passing node url and headers (optional) to constructor
$nodeUrl = 'http://127.0.0.1:7777';
$headers = array('Authorization' => 'Bearer 6ae6c8b31f09df244019ffef60c274e4'); // Optional
$client = new Casper\Rpc\RpcClient($nodeUrl, $headers);
You can find all RpcClient methods on the RpcClientAPI page. Here you can see a several of examples of using RpcClient. All examples below are supposed to be ran against the Testnet
$deploy = $client->getDeploy('fa815fc43c38da30f6ab4e5a6c8a1b31f09df2bf4b344019ffef60c1270d4e49');
$deployHeader = $deploy->getHeader();
$creationTime = $deployHeader->getTimestamp();
$auctionState = $client->getAuctionState();
$stateRootHash = $auctionState->getStateRootHash();
$blockHeight = $auctionState->getBlockHeight();
$peers = $client->getPeers();
foreach ($peers as $peer) {
...
}
$latestBlock = $client->getLatestBlock();
$latestBlockHash = $latestBlock->getHash();
- RpcClient
- putDeploy
- getDeploy
- getBlockByHash
- getBlockByHeight
- getLatestBlock
- getPeers
- getStatus
- getAuctionState
- getStateRootHash
- getAccount
- getAccountBalance
- getAccountBalanceUrefByAccountHash
- getAccountBalanceUrefByPublicKey
- getBlockState
- getBlockTransfers
- getEraSummaryBySwitchBlockHash
- getEraSummaryBySwitchBlockHeight
- getDictionaryItemByURef
- getGlobalStateByBlock
- getGlobalStateByStateRootHash
- queryBalance
- getChainspecInfo
- speculativeExecution
Run the following command from the project directory.
Replace http://127.0.0.1:7777
by the any Testnet node url before running
export CASPER_PHP_SDK_TEST_NODE_URL="http://127.0.0.1:7777" && php vendor/bin/phpunit tests