Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
client-example.php
Go to the documentation of this file.
1 <?php
2 
7 
11 define('IS_PUBLIC', TRUE);
12 
13 require_once __DIR__ . '/examples.inc.php';
14 
18 $hosts = [
19  // Start testrpc, geth or parity locally.
20  'http://localhost:7545',
21  // This is a demo-only purpose account only.
22  // Register your own access token. It's free!
23  // https://infura.io/#how-to
24  'https://kovan.infura.io/drupal'
25 ];
26 
27 foreach($hosts as $url)
28 {
29  try {
30  echo "<h3>What's up on $url</h3>";
31  $eth = new Ethereum($url);
32  printTable(status($eth));
33 
34  }
35  catch (\Exception $exception) {
36  echo "<p style='color: red;'>We have a problem:<br />";
37  echo $exception->getMessage() . "</p>";
38  echo "<pre>" . $exception->getTraceAsString() . "</pre>";
39  }
40  echo "<hr />";
41 
42 }
43 
52 function status($eth) {
53  $rows = [];
54 
55  $rows[] = ['<b>JsonRPC standard Methods</b>', 'Read more about <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC">Ethereum JsonRPC-API</a> implementation.'];
56  $rows[] = ["Client version (web3_clientVersion)", $eth->web3_clientVersion()->val()];
57  $rows[] = ["Listening (net_listening)", $eth->net_listening()->val() ? '✔' : '✘'];
58  $rows[] = ["Peers (net_peerCount)", $eth->net_peerCount()->val()];
59  $rows[] = ["Protocol version (eth_protocolVersion)", $eth->eth_protocolVersion()->val()];
60  $rows[] = ["Network version (net_version)", $eth->net_version()->val()];
61  $rows[] = ["Syncing (eth_syncing)", $eth->eth_syncing()->val() ? '✔' : '✘'];
62 
63  // Mining and Hashrate.
64  $rows[] = ["Mining (eth_mining)", $eth->eth_mining()->val() ? '✔' : '✘'];
65 
66  $hash_rate = $eth->eth_hashrate();
67  $mining = is_a($hash_rate, 'EthQ') ? ((int) ($hash_rate->val() / 1000) . ' KH/s') : '✘';
68  $rows[] = ["Mining hashrate (eth_hashrate)", $mining];
69 
70  // Gas price is returned in WEI. See: http://ether.fund/tool/converter.
71  $price = $eth->eth_gasPrice()->val();
72  $price = $price . 'wei ( ≡ ' . number_format(($price / 1000000000000000000), 8, '.', '') . ' Ether)';
73  $rows[] = ["Current price per gas in wei (eth_gasPrice)", $price];
74 
75  // Blocks.
76  $rows[] = ["<b>Block info</b>", ''];
77  $block_latest = $eth->eth_getBlockByNumber(new EthBlockParam('latest'), new EthB(FALSE));
78  $rows[] = [
79  "Latest block age",
80  date(DATE_RFC850, $block_latest->getProperty('timestamp')),
81  ];
82 
83  // Testing_only.
84 
85  $block_earliest = $eth->eth_getBlockByNumber(new EthBlockParam(1), new EthB(FALSE));
86  $rows[] = [
87  "Age of block number '1' <br/><small>The 'earliest' block has no timestamp on many networks.</small>",
88  $block_earliest->getProperty('timestamp'),
89  ];
90  $rows[] = [
91  "Client first (eth_getBlockByNumber('earliest'))",
92  '<div style="max-width: 800px; max-height: 120px; overflow: scroll">' . $eth->debug('', $block_earliest) . '</div>',
93  ];
94 
95  // Second param will return TX hashes instead of full TX.
96  $block_latest = $eth->eth_getBlockByNumber(new EthBlockParam('earliest'), new EthB(FALSE));
97  $rows[] = [
98  "Client first (eth_getBlockByNumber('latest'))",
99  '<div style="max-width: 800px; max-height: 120px; overflow: scroll">' . $eth->debug('', $block_latest) . '</div>',
100  ];
101  $rows[] = [
102  "Uncles of latest block",
103  '<div style="max-width: 800px; max-height: 120px; overflow: scroll">' . $eth->debug('', $block_latest->getProperty('uncles')) . '</div>',
104  ];
105 
106  $high_block = $eth->eth_getBlockByNumber(new EthBlockParam(999999999), new EthB(FALSE));
107  $rows[] = [
108  "Get hash of a high block number<br /><small>Might be empty</small>",
109  $high_block->getProperty('hash'),
110  ];
111 
112 
113  // Accounts.
114  $rows[] = ["<b>Accounts info</b>", ''];
115  $coin_base = $eth->eth_coinbase()->hexVal();
116  if ($coin_base === '0x0000000000000000000000000000000000000000') {
117  $coin_base = 'No coinbase available at this network node.';
118  }
119 
120  $rows[] = ["Coinbase (eth_coinbase)", $coin_base];
121  $address = ['No accounts available.'];
122  $accounts = $eth->eth_accounts();
123  if (count($accounts)) {
124  $address = [];
125  foreach ($eth->eth_accounts() as $addr) {
126  $address[] = $addr->hexVal();
127  }
128  }
129  $rows[] = ["Accounts (eth_accounts)", implode(', ', $address)];
130 
131  // More.
132  $rows[] = [
133  "web3_sha3('Hello World')",
134  // Using the API would be: $eth->web3_sha3(new EthS('Hello World'))->hexVal(),
135  $eth->sha3('Hello World'),
136  ];
137 
138  // NON standard JsonRPC-API Methods below.
139  $rows[] = ['<b>Non standard methods</b>', 'PHP Ethereum controller API provides additional methods. They are part of the <a href="https://github.com/digitaldonkey/ethereum-php">Ethereum PHP library</a>, but not part of JsonRPC-API standard.'];
140 
141  $rows[] = ["getMethodSignature('validateUserByHash(bytes32)')", $eth->getMethodSignature('validateUserByHash(bytes32)')];
142 
143  return $rows;
144 }
if(!IS_PUBLIC) printTable($rows)
foreach($hosts as $url) status($eth)
$hosts
Definition: Abi.php:3