首先安装一个testrpc,用npm来安装。

  1. # 安装 testrpc
  2. npm install -g ethereumjs-testrpc //testrpc现在用ganache-cli来替代了,npm install -g ganache-cli
  3. # 安装 truffle #这个可以通过truffle进行查看当前的链内容,也可以通过truffle发币
  4. npm install -g truffle

testrpc模拟一个本地的私有链:testrpc -p 8646 -i 5266

  • -p 表示监听8646端口
  • -i 表示networkid为5266,ethereum主网id为1.

curl发送post请求,就可以看到我们用testrpc启动后,系统自动创建的10个账户

  1. //获取所有account
  2. curl 127.0.0.1:8646 -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_accounts", "params":[]}' -H "Content-type: application/json;charset=UTF-8"

curl发送post请求,就可以看到0xc9fb925d5c347185ce39ce6b627268a516ce4315账户剩下多少eth。

  1. //获取单个用户的eth
  2. curl -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xc9fb925d5c347185ce39ce6b627268a516ce4315","latest"]}' 127.0.0.1:8646 -H "Content-type: application/json;charset=UTF-8"

创建一个账号:

  1. curl 127.0.0.1:8545 -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"personal_newAccount", "params":["yourAccountPassword"]}' -H "Content-type: application/json;charset=UTF-8"
  2. //yourAccountPassword是字母和数字的结合最好

来一段php代码:

  1. <?php
  2. //header("Content-type:application/json;charset=utf-8");
  3. function http_post_data($url, $data_string)
  4. {
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_POST, 1);
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  9. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  10. "Content-Type: application/json; charset=utf-8",
  11. "Content-Length: " . strlen($data_string)]
  12. );
  13. ob_start();
  14. curl_exec($ch);
  15. $return_content = ob_get_contents();
  16. ob_end_clean();
  17. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  18. return [$return_code, $return_content];
  19. }
  20. //curl -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xc9fb925d5c347185ce39ce6b627268a516ce4315","latest"]}' 127.0.0.1:8646
  21. $params = ["0x41de94fb75cc86856306433340430d1ae63dc30c", "latest"];
  22. $data = ["id" => 5266, "jsonrpc" => "2.0", "method" => "eth_getBalance", 'params' => $params];
  23. $url = '127.0.0.1:8646';
  24. $data = json_encode($data);
  25. list($return_code, $return_content) = http_post_data($url, $data); //return_code是http状态码
  26. var_dump($return_content);

怎么样?简单吧?来,鼓个掌。!!!!

注:也可以通过goeth来启动一个私链:

  1. echo "===== Starting geth node =====";
  2. set -x;
  3. nohup geth --datadir $GETH_HOME -verbosity $VERBOSITY --bootnodes $BOOTNODE_URLS --maxpeers $MAX_PEERS --nat none --networkid $NETWORK_ID --identity $IDENTITY $MINE_OPTIONS $FAST_SYNC --rpc --rpcaddr "$IPADDR" --rpccorsdomain "*" --rpcapi "eth,net,web3,admin,personal" >> $GETH_LOG_FILE_PATH 2>&1 &
  4. if [ $? -ne 0 ]; then echo "Previous command failed. Exiting"; exit $?; fi
  5. set +x;
  6. echo "===== Started geth node =====";

以太坊官方JSON-RPC:
https://github.com/ethereum/wiki/wiki/JSON-RPC
https://blogs.msdn.microsoft.com/pkirchner/2017/07/12/getting-started-with-ethereum-tutorials-on-azure/