跳至主要內容

How to connect to Ethereum using PHP

更新於
2025年11月26日

閱讀時間 5 分鐘

概覽

PHP is a very popular choice among developers and has a vast community due to its long presence in web development. In this guide, we’ll cover how to connect to Ethereum with PHP using the web3.php library.

先決條件

  • 文字編輯器
  • CLI

什麼是 PHP?

PHP, which initially stood for Personal Home Page, is now a recursive acronym for PHP: Hypertext Preprocessor. It is a general-purpose scripting language most commonly used for building back-ends of websites. PHP code is processed on the server-side by a PHP interpreter. PHP can be embedded in HTML, used in standalone files, and on the command line. It is open-source and cross-platform compatible. PHP 7+ is speedy,  used by nearly 80% of websites, and has active development. It is high in demand and can be used to make dynamic websites, template systems, process user-submitted data, e-commerce websites, etc.

**Why use PHP?
**

  • PHP is mature, well tested, and predictable.
  • It has lots of open source libraries so that you can do many things with it right away.
  • Though it is derived from the C++ family, PHP codes are comparatively shorter.
  • It Integrates easily into the HTML/CSS stack, unlike other languages.
  • It has many impressive frameworks such as Symfony and Laravel.
  • It has a short learning curve when compared to JSP and ASP.
  • It allows developers to achieve many complex things very quickly and easily.

**Characteristics of PHP?
**

  • Scalability - helps to build scalable web applications.
  • Flexibility - Works with major operating systems.
  • Simplicity - Easy and straightforward to use compared to other scripting languages.
  • Case Sensitive - PHP is a case-sensitive language at the time of variable declaration. All keywords, classes, functions, and user-defined functions are not case-sensitive.
  • Loosely typed - In PHP, variables can be declared without the data type. It’s determined at the time of interpretation based on the kind of data it contains.
  • Interpreted - PHP is an interpreted language, which means there’s no need for compilation.
  • Open Source - It is open source, which means free to use and well maintained.

What is web3.php?

Web3.php is a PHP interface for connecting and interacting with the Ethereum blockchain. It is open-source, and can be used to get blockchain data and interact with deployed contracts. It has inbuilt support for eth, web3, net, and shh JSON RPC modules of Ethereum.

For our purpose today, we’ll fetch the latest block number of the Ethereum blockchain to demonstrate a connection between our code and Ethereum. 

We’ll use web3.php and an Etherum node. 

Installing PHP and web3.php

Before installing the web3.php, let’s check if we have PHP installed on our system. To do so, copy-paste and run the following in your terminal/cmd.

php -v

It should return the PHP version. If not installed, download the operating system-specific PHP by following the instructions on the official PHP website’s download page.

We’ll need to install the PHP gmp extension. You can either uncomment it from the php.init file or install it manually using the following.

sudo apt-get install php-gmp

注意:我們應僅採用上述方法中的一種來啟用 gmp。 

適用於 Mac:

Reinstalling PHP with brew can help to enable the gmp extension. 

Run the following commands:

brew reinstall php@7.2
export PATH="/usr/local/opt/php@7.2/bin:$PATH"
brew services restart php@7.2
php -info | grep "GMP"

We’ll use composer, a tool to manage PHP libraries/dependencies. Check if composer is installed on your system by running the following in your terminal/cmd:

composer 

若尚未安裝,請參照Composer 網站的下載頁面進行下載與安裝。

現在請在您的專案目錄中建立一個名為 composer.json 的 JSON 檔案,並將以下內容複製並貼上至該檔案中。

{
"require": {
"sc0vu/web3.php": "dev-master"
}
}

Then run the following command to install the required dependencies in the directory.

composer install

Note: If you encounter any error on Linux at this point, try installing the following dependency: 

sudo apt-get install php-mbstring

設定您的 Quicknode 以太坊端點

We could use pretty much any Ethereum client; such as Geth or OpenEthereum (fka Parity), for our purposes today. Since that is a bit too involved for just querying the block height, we'll create a free Quicknode account here and easily generate an Ethereum endpoint. It can be a testnet (like Goerli or Sepolia) or Mainnet. After you've created your free Ethereum endpoint, copy your HTTP Provider endpoint:

 

Quicknode 以太坊端點「入門指南」頁面的螢幕截圖,其中包含一個 HTTP 連結和 WSS

 

稍後你會需要這個,所以請複製並儲存下來。

Connecting to Ethereum with PHP

Now, make a PHP script file index.php in your project directory and copy-paste the following in it.

<?

require_once "vendor/autoload.";

use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

$web3 = new Web3(new HttpProvider(new HttpRequestManager("ADD_YOUR_ETHEREUM_NODE_URL")));

$eth = $web3->eth;

$eth->blockNumber(function ($err, $data) {
echo "Latest block number is: ". $data . " \n";
});
?>

Make sure to replace `ADD_YOUR_ETHEREUM_NODE_URL` with the HTTP provider from the section above.

上述程式碼的說明。

第 1 行:以 PHP 開啟標籤開始 PHP 腳本。

Line 3-7: Importing the installed dependencies.

Line 9: Initializing the web3 instance, a HttpProvider instance, and supplying the node URL.

Line 11: Making a new variable eth and connecting the web3 instance with eth to use eth methods using our node.

Line 13: Getting the latest block number from the chain using the blockNumber() method, which under the hood uses eth_blockNumber. We then print the block number along with a string.

Save the file and run the PHP script by typing the following in your terminal/cmd:

$ php index.php

If everything goes well, you will see an output that shows the latest block number.

結論

Congratulations on connecting to the Ethereum network using PHP. Now, you can take advantage of the Ethereum network by integrating it into your server-side applications, making them more secure and robust. 

Learn How to generate a new Ethereum address in PHP and connect to the Ethereum network using various programming languages.

訂閱我們的電子報,獲取更多關於以太坊的文章與指南。如有任何意見回饋,歡迎透過Twitter 聯絡我們。您隨時都可以在我們的Discord社群伺服器上與我們聊天,那裡聚集了您這輩子見過最酷的開發者們 :)

分享這份指南