A very basic Ethereum client for PHP; with strict typehints for arguments and return values, for more info on methods see the RPC documentation.
Based on the schema description in ethjs this client offers a way to connect to any parity or geth server using PHP.
// Simple autoloader to find Ethereum & Rpc client
spl_autoload_register(function($class){
$class = str_replace('\\', '/', $class);
if (0 === strpos($class, 'TooBasic/Ethereum/'))
require('TooBasic-Ethereum/'. substr($class, strlen('TooBasic/Ethereum/')) .'.php');
elseif (0 === strpos($class, 'TooBasic/Rpc/'))
require('TooBasic-Rpc/'. substr($class, strlen('TooBasic/Rpc/')) .'.php');
elseif (file_exists($class .'.php'))
require($class .'.php');
});
// Setup an RPC connection with a curl transport
$curl = new TooBasic\Rpc\Transport\Curl;
self::$client = new TooBasic\Ethereum\Client('http://127.0.0.1:8546', $curl);
self::$client->setNamedParameters(false);
// You can call any method your ethereum node supports
var_dump('Local sync-status: ', self::$client->eth_syncing());
var_dump('Local last-block:', self::$client->eth_blockNumber());
var_dump('Foundation tip-jar balance (in wei):', self::$client->eth_getBalance(
new Schema\Primitive\Bytes20('fB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'),
new Schema\Primitive\QuantityOrTag(Schema\Tag::LATEST)
));
// Show the contents of a complete block including all transactions:
var_dump(self::$client->eth_getBlockByNumber(new Schema\Primitive\QuantityOrTag(dechex(4000000)), true));
- Bytes / Bytes20 / Bytes32
- Quantity / PaddedQuantity (with
bcmath
/gmp
as backend for large numbers) - boolean - supported by using native PHP booleans
- string - supported by using native PHP booleans
Since the API has a few methods that require or return different primitives, we have 3 wrappers to represent those:
- BooleanOrEthSyncing
- ArrayOrData
- QuantityOrTag
The RPC describes a single Tag Type which is neither a Primitive nor an Object
- Tag - a wrapper for the latest/earliest/pending tag
Since I parse the schema and generate all objects dynamically, this means you can regenerate all Objects using php ClientGenerator.php
. Currently this generates:
- EthSyncing
- Block
- Receipt
- Transaction
- CallTransaction
- EstimateTransaction
- SendTransaction
- Filter
- FilterChange
- SHHMessage
- SHHPost
- SHHFilterChange
- SHHFilter