1. Go to this page and download the library: Download steven-ok/simple-web3-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/ */
steven-ok / simple-web3-php example snippets
use SWeb3\SWeb3;
//initialize SWeb3 main object
$sweb3 = new SWeb3('http://ethereum.node.provider:optional.node.port');
//optional if not sending transactions
$from_address = '0x0000000000000000000000000000000000000000';
$from_address_private_key = '345346245645435....';
$sweb3->setPersonalData($from_address, $from_address_private_key);
use SWeb3\Utils;
$res = $sweb3->call('eth_blockNumber', []);
$bigNum = Utils::hexToBn($res->result);
//remember to set personal data first with a valid pair of address & private key
$sendParams = [
'from' => $sweb3->personal->address,
'to' => '0x1111111111111111111111111111111111111111',
'gasLimit' => 210000,
'value' => Utils::toWei('0.001', 'ether'),
'nonce' => $sweb3->personal->getNonce()
];
$result = $sweb3->send($sendParams);
//enable batching
$sweb3->batch(true);
$sweb3->call('eth_blockNumber', []);
$sweb3->call('eth_getBalance', [$sweb3->personal->address, 'latest']);
//execute all batched calls in one request
$res = $sweb3->executeBatch();
//batching has to be manually disabled
$sweb3->batch(false);
use SWeb3\Accounts;
use SWeb3\Account;
//create new account privateKey/address (returns Account)
$account = Accounts::create();
//retrieve account (address) from private key
$account2 = Accounts::privateKeyToAccount('...private_key...');
//sign message with account
$res = $account2->sign('Some data');
use SWeb3\SWeb3_Contract;
$contract = new SWeb3_contract($sweb3, '0x2222222222222222222222222222222222222222', '[ABI...]'); //'0x2222...' is contract address
// call contract function
$res = $contract->call('autoinc_tuple_a');
// change function state
//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId
$extra_data = ['nonce' => $sweb3->personal->getNonce()];
$result = $contract->send('Set_public_uint', 123, $extra_data);
//optional parameters fromBlock, toBlock, topics
//default values -> '0x0', 'latest', null (all events/logs from this contract)
$res = $contract->getLogs();
use SWeb3\SWeb3_Contract;
$creation_abi = '[abi...]';
$contract = new SWeb3_contract($sweb3, '', $creation_abi);
//set contract bytecode data
$contract_bytecode = '123456789....';
$contract->setBytecode($contract_bytecode);
//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId
$extra_params = ['nonce' => $sweb3->personal->getNonce()];
$result = $contract->deployContract( [123123], $extra_params);
use SWeb3\SWeb3; //always needed, to create the Web3 object
use SWeb3\Utils; //sweb3 helper classes (for example, hex conversion operations)
use SWeb3\SWeb3_Contract; //contract creation and interaction
use SWeb3\Accounts; //account creation
use SWeb3\Account; //single account management (signing)
use phpseclib\Math\BigInteger as BigNumber; //BigInt handling
use stdClass; //for object interaction
//swp_contract.sol is available on ropsten test net, address: 0x706986eEe8da42d9d85997b343834B38e34dc000
define('SWP_Contract_Address','0x706986eEe8da42d9d85997b343834B38e34dc000');
$SWP_Contract_ABI = '[...]';
define('SWP_Contract_ABI', $SWP_Contract_ABI);