This post was last edited by malakashi on 2018-8-25 23:11
Since last year, we have been developing a blockchain business. Recently used Ethereum and used PHP, so I thought we should talk about this topic. Here's a premise: - 1. Understand blockchain
- 2. Have an understanding of programming languages
Key takeaways from this article: - 1. Development environment
- 2. Introduction of Ethereum
- 3. Prepare the JSON-RPC call
- 4. Access from PHP
1. Development environmentWe will be using Ubuntu 14.04 LTS. After installing the operating system, enter the predetermined commands. After that I set up SSH, iptables, ntp, etc. Then there's Apache + PHP. The PHP version will be 5.5.
2. Introduction of EthereumThis time we will use an Ethereum node made in GO language, go-ethereum abbreviated as geth. Let's add a repository first. After that, just install it. Launch Geth as soon as the installation is complete. First, create a data catalog and describe the settings of the first block (genesis block). Next, create a Genesis block and start geth. When in console mode, the startup is successful. Several options are specified, and the explanation is summarized below. - networkid, which connects to a public node.
- port, which is a port for standby. I changed it to a 4-digit number.
- nodiscover, which is a setting that prevents nodes from automatically viewing.
- datadir, specifying the directory where the blockchain stores data.
- console, which launches the console at the same time as it starts.
Okay, prepare for the next call of PHP, but before that let's do more preparation on the geth side. 3. Prepare the JSON-RPC callAccess from PHP via JSON-RPC. Here we will set up the neighborhood. First, the current account information will be obtained. We also use the geth console that we launched last time. We haven't created an account yet, so it's necessary to create it. The account is now created. We set it up so that rewards can be earned while mining. Next, since the current node does not allow RPC calls, add an option and start it again. Let's end the geth first. Let's add options and restart Several options for RPC have been added. The explanation is as follows. - rpc, allowing RPC to standby.
- rpcaddr, an IP address for RPC alternates.
- rpcport, the port used to listen to RPC. I lowered the port number to 4 digits.
- rpccorsdomain, a domain that allows access to RPC. Note that when publishing a node, if it's "*", it will allow everything.
The node side is now ready. Next will be a call from PHP. 4. Access from PHPFinally, why access from PHP, but have them, because it's quite difficult and can be used when you write codeThis one。 As you can see in the README, you just need to create a class. Let's use it by putting it in the same directory as the test script. When we run this script, there should be a list of accounts as follows.
If you can't connect, check the port settings, etc. So far, we've explained it briefly, but doesn't it feel surprisingly easy to develop with PHP? When it comes to smart contracts, it gets a bit more complicated, and we'll write about it later. Also, if you want to see directly how blockchain is used in Ethereum, you can check this outhttp://xc.hubwiz.com/course/5b36629bc02e6b6a59171de3?affid=825itsvse, mainly introduces the use of PHP for smart contract development interaction, account creation, trading, transfer, token development, filters and events, etc.
|