Marketplace has launched, further enabling blockchain developers! Learn more

How to connect to Ethereum using PHP

September 09, 2022


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.

  • A text editor
  • CLI

What is 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.

installing php and web3 php

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.

installing php and web3 php

sudo apt-get install php-gmp

Note: We should enable gmp using only one of the methods mentioned above. 

For mac:

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

Run the following commands:

installing php and web3 php

brew reinstall [email protected]
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
brew services restart [email protected]
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:

installing php and web3 php


If not installed, download and install it by following the Composer website’s downloads page.

Now create a JSON file named composer.json in your project directory and copy-paste the following in it.

installing php and web3 php

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

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

installing php and web3 php

composer install

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

installing php and web3 php

sudo apt-get install php-mbstring

Booting our Ethereum node

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 grab a free trial endpoint from QuickNode to make this easy. It can be a testnet (like Ropsten, Rinkeby or Kovan) or Mainnet. After you've created your free Ethereum node, copy your HTTP Provider endpoint:
A screenshot of the Quicknode Ethereum endpoint Getting Started page with an HTTP link and WSS

You'll need this later, so copy and save it.

Connecting to Ethereum with PHP

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

connecting to ethereum with php


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.

Explanation of the code above.

Line 1: Starting the PHP script with PHP opening tag.

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:

connecting to ethereum with php

$ 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. 

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 35

How to do a non-custodial transaction with QuickNode
Originally Published On: Jun 19, 2021
Updated On: Sep 9, 2022

Private keys are one of the most... A non-custodial wallet is a... Custodian of Private... At We could use pretty much any... We could use any of the many... Let us create a token transfer... Congratulations on making a...

Continue reading
How to Set Up a Near Project from Scratch
Originally Published On: Jan 27, 2022
Updated On: Sep 9, 2022

In this tutorial we will look at... In this section, we will... In this section, we will look at... In this section, we will look at... When writing smart contracts on... In the last section, we looked... In the last section, we looked... In the last section, we looked... We have finally come to the end...

Continue reading
Estimating gas price using pending transactions in Python
Originally Published On: Feb 20, 2021
Updated On: Sep 9, 2022

To send a transaction on the... Gas is the unit to measure the... Why do we want to estimate gas?... Our first step here would be to... For our purposes today, we could... The first thing we’re going to... Now you know how to estimate gas...

Continue reading
Como crear y lanzar un ERC-721 (NFT)
Originally Published On: Dec 27, 2021
Updated On: Sep 9, 2022

Coleccionables digitales que son... Fungible significa que es lo... ERC es la abreviación para... Ahora que conocemos qué es y... Antes de escribir nuestro... Felicidades por crear tu propio...

Continue reading
How to Access Bitcoin Mempool
Originally Published On: Aug 16, 2021
Updated On: Sep 21, 2022

Bitcoin is the father of... The transactions which are sent... For our purpose today, we need a... We will use Bitcoin RPC methods... Congratulations on mastering the...

Continue reading
How to Mint an NFT on Polygon with Ethers.js
Originally Published On: Mar 28, 2022
Updated On: Sep 9, 2022

This guide demonstrates how to... Ethers.js is a complete Ethereum... As a reminder, the act of... We will deploy our contract on... Time to write the code! Start by... Note, you will need some... That’s it! You have minted an...

Continue reading
How to access Ethereum Mempool
Originally Published On: Oct 15, 2020
Updated On: Sep 15, 2022

On Ethereum, when a transaction... In blockchain terminology, a... For the purpose of this guide,... First, query the Let’s examine how to subscribe... This short guide showed you how...

Continue reading
The Web3 Developer Stack
Originally Published On: Jul 3, 2021
Updated On: Sep 15, 2022

A developer stack is a bag of... If we can imagine web3 as a car... The Chainlink price feed... Head over to the We’ll need a Kovan node to... We’ll now write a short... Congratulations :D You’ve taken...

Continue reading
How to Mint NFTs on the Stacks Blockchain
Originally Published On: Aug 19, 2022
Updated On: Sep 9, 2022

Stacks is an emerging blockchain... The Stacks ecosystem has adopted... Open up a terminal window and... Implementing the SIP-009... This section will demonstrate... The moment you've been waiting... Awesome Work! You’ve learned how...

Continue reading
Introduction to Scaffold-ETH 🏗
Originally Published On: Oct 7, 2021
Updated On: Sep 9, 2022

Developing applications involves... First of all, before getting... Scaffold-ETH comes with a demo... We will set up a QuickNode... Congratulations, and thank you...

Continue reading
How to use Subspace with QuickNode
Originally Published On: Sep 8, 2020
Updated On: Sep 9, 2022

In this guide, we'll understand... To install Subspace, we'll need... For our purposes today, we could... Now let's create a short script,... Run the file using the node...

Continue reading
How to Connect Your Dapp With MetaMask Using Ethers.js
Originally Published On: Dec 13, 2021
Updated On: Sep 9, 2022

In our dApp, we will have a... A dApp is an application that... We will need to install the... Ethers.js is a lightweight... With this application, we will... Next, we will create a function... After connecting to MetaMask, we... To handle the wallet address of... This section will show us how to... In this tutorial, we...

Continue reading
How to Lazy Mint an NFT on Rarible with Rarepress
Originally Published On: Feb 23, 2022
Updated On: Sep 9, 2022

NFTs are great for creators to... To mint an NFT, you write data... Lazy Minting is a process in... We will write some JavaScript... Rarepress interacts with the... Now that we have everything in... Congratulations on listing your...

Continue reading
How to get the balance of an ERC-20 token
Originally Published On: Jun 14, 2021
Updated On: Sep 21, 2022

When a new token is made on the... You could use any Ethereum... Now that you've done the legwork... Getting the ERC20-Token Balance... In the top of your... ABI is short for You will most likely interact... You can use a similar process... We now have a connection to an... Having made it to the end, you...

Continue reading