Table of Contents
1. Development environment
2. Introduction to Ethereum
3. Prepare for JSON-RPC calls
4. Access from PHP
Home Backend Development PHP Tutorial PHP implements content parsing for interaction with Ethereum through JSON-RPC

PHP implements content parsing for interaction with Ethereum through JSON-RPC

Aug 23, 2018 am 10:03 AM
django laravel php Ethereum Blockchain

The content of this article is about the content analysis of PHP interacting with Ethereum through JSON-RPC. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Since last year, we are developing blockchain (Blockchain) business. Having recently used Ethereum and working with PHP, I thought we'd chat about this topic.

There is a premise here:

  • 1. Understand the blockchain

  • 2. Have an understanding of programming languages

Text:

1. Development environment

We will use Ubuntu 14.04 LTS. After installing the operating system, enter the predetermined commands.

$ sudo apt-get update
$ sudo apt-get upgrade
Copy after login

After that I set up SSH, iptables, ntp, etc.

Then comes Apache PHP. The PHP version will be 5.5.

$ sudo apt - get install php 5 libapache 2 - mod - php 5 php 5 - curl
Copy after login

2. Introduction to Ethereum

This time we will use the Ethereum node made in GO language, go-ethereumreferred to as geth.

First let's add a repository.

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum / ethereum
$ sudo add-apt-repository -y ppa:ethereum / ethereum-dev
Copy after login

After that, just install it.

$ sudo apt-get update
$ sudo apt-get install ethereum
Copy after login

Start geth immediately after the installation is completed.
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”,
“难度”:“0x4000”,
“mixhash”:“0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”:“0x3333333333333333333333333333333333333333”,
“alloc”:{}
}
Copy after login

Next, create the 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
Copy after login

When in console mode, the startup is successful.

Specifies several options, the explanations are summarized below.

  • networkid, connect to public node.

  • port, it is a port used for standby. I changed it to 4 digits.

  • nodiscover, this is a setting that prevents nodes from automatically viewing.

  • datadir, specifies the directory where the blockchain stores data.

  • console, start the console at the same time.

Okay, let’s prepare for the next call to PHP, but before that let’s do a little more preparation on the geth side.

3. Prepare for JSON-RPC calls

Access 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 we last started.

> eth.accounts
[]
Copy after login

We haven't created an account yet, so we need to create it.

> personal.newAccount("password")
"0xb83fa0d1c6b34a42f900cca5a32400c3b6f69f4b"
> eth.accounts
["0xb83fa0d1c6b34a42f900cca5a32400c3b6f69f4b"]
Copy after login

Account now created. We set it up so that we can get rewards when mining.

> miner.setEtherbase(eth.accounts [0])
Copy after login

Next, since the current node does not allow RPC calls, add an option and start it again. First let's wrap up geth.

> exit
Copy after login

Let’s add options and restart

$ geth --networkid 57598955 --port 8955 --nodiscover --rpc --rpcaddr "0.0.0.0" --rpcport "8956" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" --datadir "/home/yoshida/eth_private_net" console 2>> /home/yoshida/eth_private_net/geth_err.log
Copy after login

Added several options for rpc. The explanation is as follows.

  • rpc, allows RPC backup.

  • rpcaddr, used for RPC backup IP address.

  • rpcport, the port used to listen to RPC. I lowered the port number to 4 digits.

  • rpccorsdomain, the domain that allows access to RPC. Note that when publishing a node, if it is "*" it will allow everything.

The node side is now ready. Next will be the call from PHP.

4. Access from PHP

The last thing is why access from PHP but have them because it is quite difficult to use this when you write code.

As you can see in the README, you only 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 -l
ethereum.php json-rpc.php
$ vim test.php
<?php

require_once &#39;ethereum.php&#39;;
$ethereum = new Ethereum(&#39;localhost&#39;, &#39;8956&#39;);
print_r($ethereum->eth_accounts());
Copy after login

When we run this script, there should be a list of accounts as follows.

$ php test.php
Array
(
[0] => 0xb83fa0d1c6b34a42f900cca5a32400c3b6f69f4b
)
Copy after login

If you cannot connect, please check the port settings, etc.

So far, we have explained it simply, but does it feel like developing with PHP is surprisingly easy?

Related recommendations:

How the browser obtains relevant data through the JSON-RPC interface of the Bitcoin Core client

A simple json rpc framework example implemented in php

The above is the detailed content of PHP implements content parsing for interaction with Ethereum through JSON-RPC. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
The TOP5 of the safest exchanges in 2025: Black U's guide to avoid pits, the rule of 100% of funds to save lives The TOP5 of the safest exchanges in 2025: Black U's guide to avoid pits, the rule of 100% of funds to save lives May 08, 2025 pm 08:27 PM

In the field of cryptocurrency trading, the security of exchanges has always been the focus of users. In 2025, after years of development and evolution, some exchanges stand out with their outstanding security measures and user experience. This article will introduce the five most secure exchanges in 2025 and provide practical guides on how to avoid Black U (hacker attacks users) to ensure your funds are 100% secure.

How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 May 08, 2025 pm 07:15 PM

According to the latest evaluations and industry trends from authoritative institutions in 2025, the following are the top ten cryptocurrency platforms in the world that support multi-chain transactions, combining transaction volume, technological innovation, compliance and user reputation comprehensive analysis:

Ethereum (ETH) Market Analysis and Trading Strategy: May 8, 2025 Ethereum (ETH) Market Analysis and Trading Strategy: May 8, 2025 May 08, 2025 pm 08:12 PM

Ethereum's current market profile and short-term trading strategy

TOP10 futures trading platforms: Perpetual contracts and options trading TOP10 futures trading platforms: Perpetual contracts and options trading May 08, 2025 pm 07:12 PM

In the cryptocurrency market, futures trading platforms play an important role, especially in perpetual contracts and options trading. Here are the top ten highly respected futures trading platforms in the market, and provide detailed introduction to their characteristics and advantages in perpetual contract and option trading.

PHP Email: Step-by-Step Sending Guide PHP Email: Step-by-Step Sending Guide May 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

Strategy for making money with zero foundation: 5 types of altcoins that must be stocked in 2025, make sure to make 50 times more profitable! Strategy for making money with zero foundation: 5 types of altcoins that must be stocked in 2025, make sure to make 50 times more profitable! May 08, 2025 pm 08:30 PM

In cryptocurrency markets, altcoins are often seen by investors as potentially high-return assets. Although there are many altcoins on the market, not all altcoins can bring the expected benefits. This article will provide a detailed guide for investors with zero foundation, introducing the 5 altcoins worth hoarding in 2025, and explaining how to achieve the goal of making a 50x steady profit through these investments.

Go: String Manipulation with the Standard 'strings' Package Go: String Manipulation with the Standard 'strings' Package May 09, 2025 am 12:07 AM

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

See all articles