This article brings the content is about PHP through the JSON-RPC implementation and ethereum Interactive content analysis, there is a certain reference value, the need for friends can refer to, I hope to help you.
We are developing the blockchain (Blockchain) business since last year. Recently used Ethereum and PHP, so I think we should talk about this topic.
Here's a premise:
Body:
1. Development environment
We will use Ubuntu 14.04 LTS. After installing the operating system, enter the pre-determined command.
$ sudo apt-get update$ sudo apt-get upgrade
Then I set the SSH,IPTABLES,NTP and so on.
Then Apache + PHP. The PHP version will be 5.5.
$ sudo apt-get install PHP 5 libapache 2-mod-php 5 php 5-curl
2. Introduction to Ethereum
This time we will use the go language to make the Ethereum node, referred to as go-ethereum
Geth.
Let's start by adding a repository.
$ sudo apt-get install software-properties-common$ sudo add-apt-repository-y ppa:ethereum/ethereum$ sudo add-apt-reposi Tory-y Ppa:ethereum/ethereum-dev
After that, just install it.
$ sudo apt-get update$ sudo apt-get install Ethereum
Start Geth immediately after installation is complete.
First, create a data directory and describe the settings for the first block (the Genesis block).
$ mkdir~/eth_private_net$ vim~/eth_private_net/my_genesis.json{"nonce": "0x0000000000000042", "timestamp": "0x0", " Parenthash ":" 0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 "," Extradata ":" 0x0 "," Gaslimit ":" 0xFFFFFFFF "," difficulty ":" 0x4000 "," Mixhash ":" 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 "," Coinbase ":" 0x3333333333333333333333333333333333333333 "," Alloc ": {}}
Next, create a Genesis block and start Geth.
$ geth--datadir "/home/yoshida/eth_private_net" init/home/yoshida/eth_private_net/my_genesis.json$ geth--networkid 57598955--port 8955--nodiscover--datadir "/home/yoshida/eth_private_net" Console 2>>/home/yoshida/eth_ Private_net/geth_err.log
When in console mode, startup succeeds.
Several options have been specified and the explanations are summarized below.
Networkid, connect to the public node.
Port, which is the one used for standby. I changed it to a 4-digit number.
Nodiscover, which is a setting that prevents nodes from automatically viewing.
DataDir, specifies the directory where the blockchain stores data.
Console, start the control console while booting.
Okay, prepare for the next call to PHP, but let's get more prepared for Geth.
3. Prepare the JSON-RPC call
Accessed from PHP via JSON-RPC. Here, we will set the neighborhood.
First, you get the current account information. We also use the Geth console that was last started.
> eth.accounts[]
We haven't created an account yet, so we'll need to create it.
> Personal.newaccount ("Password") "0xb83fa0d1c6b34a42f900cca5a32400c3b6f69f4b" > eth.accounts[" 0XB83FA0D1C6B34A42F900CCA5A32400C3B6F69F4B "]
The account is now created. We set it up so that we can get a reward when we dig mine.
> miner.setetherbase (eth.accounts [0])
Next, because the current node does not allow RPC calls, add an option and start it again. First let's end the Geth.
> Exit
Let's add options and restart
$ geth--networkid 57598955--port 8955--nodiscover--rpc--rpcaddr "0.0.0.0"--rpcport "8956"--rpccorsdomain "*"--RPCA Pi "eth,net,web3,personal"--datadir "/home/yoshida/eth_private_net" Console 2>>/home/yoshida/eth_private_net /geth_err.log
There are several options for adding RPC. Explained below.
RPC, which allows RPC backup.
RPCADDR, the IP address used for RPC standby.
Rpcport, the port used to listen for RPC. I lowered the port number to 4 digits.
Rpccorsdomain, which allows access to the RPC domain. Note that when the node is published, it will allow all content if it is "*".
The node side is now ready. The next step will be a call from PHP.
4. Access from PHP
The last is why access from PHP, but have them, because this is quite difficult, when you write code can use this.
As you can see in the Readme, you just need to create a class.
Let's use it by placing it in the same directory as the test script.
$ cd/home/yoshida/php-eth/$ ls-lethereum.php json-rpc.php$ vim test.php<?phprequire_once ' ethereum.php '; $ethereum = new Ethereum (' localhost ', ' 8956 ');p rint_r ($ethereum->eth_accounts ());
When we run this script, there should be an account list as follows.
$ php Test.phparray ([0] = 0xb83fa0d1c6b34a42f900cca5a32400c3b6f69f4b)
If you cannot connect, check the port settings and so on.
So far, we have simply explained it, but did it feel surprisingly easy to use PHP development?