⚡️ Web3 PHP is a supercharged PHP API client that allows you to interact with a generic Ethereum RPC.

Overview

Web3 PHP

GitHub Workflow Status (master) Total Downloads Latest Version License


Web3 PHP is a supercharged PHP API client that allows you to interact with a generic Ethereum RPC.

This project is a work-in-progress. Code and documentation are currently under development and are subject to change.

Get Started

Requires PHP 8.0+

First, install Web3 via the Composer package manager:

composer require web3-php/web3

Then, interact with a local or remote ethereum node:

use Web3\Web3;

$web3 = new Web3('http://127.0.0.1:8545');

$accounts = $web3->eth()->accounts(); // ['0x54a3259f4f693e4c1e9daa54eb116a0701edc403', ...]

Usage

Web3 Namespace

clientVersion

The clientVersion method returns the version of the current client.

$web3->clientVersion(); // TestRPC v2.13.2

sha3

The sha3 method hashes data using the Keccak-256 algorithm.

$web3->sha3('string'); // 0x2bc897d8156dzd92f46392126434c0dedzb7ee31dcbcfc6s28

Eth Namespace

accounts

The accounts method returns a list of addresses owned by this client.

$web3->accounts(); // ['0x54a3259f4f693e4c1e9daa54eb116a0701edc403', ...]

gasPrice

The gasPrice method returns the current price of gas in wei.

$web3->gasPrice()->toEth(); // 0.00000002

getBalance

The getBalance method returns the balance of an address in wei.

$web3->getBalance('0x54a3259f4f693e4c1e9daa54eb116a0701edc403')->toEth(); // 100

Net Namespace

listening

The listening method determines if this client is listening for new network connections.

$web3->net()->listening(); // true

peerCount

The peerCount method returns the number of peers currently connected to this client.

$web3->net()->peerCount(); // 230

version

The version method returns the chain ID associated with the current network.

$web3->net()->version(); // 1637712995212

Web3 PHP is an open-sourced software licensed under the MIT license.

Comments
  • Remove the final keyword so we can extend the classes

    Remove the final keyword so we can extend the classes

    I would like to extend the Eth class to implement eth_call for example, but I am unable to do so because the class is final. The same applies to Web3 or even Http (I had to add Content-type into headers for my BSC RPC to work)

            $request = new Request('POST', $this->url, [
                "Content-Type" => "application/json",
            ], $body);
    

    Could you consider removing the final keyword from them?

    opened by genesiscz 7
  • improvement: improve web3_sha3 is string hex

    improvement: improve web3_sha3 is string hex

    It is not enough to just check the first two characters to make sure a string is Hex. Therefore the characters of the rest of the string (substr($value, 2) we know that the first two characters are '0x' at this point) must represent hexadecimal digits ctype_xdigit(substr($value, 2)).

    https://www.php.net/manual/en/function.ctype-xdigit.php

    opened by hammer-rot 2
  • feat: add `blockNumber` method to `Eth` namespace

    feat: add `blockNumber` method to `Eth` namespace

    Description

    • Adds blockNumber() method to Eth namespace

    Notes

    • My understanding of this method is that it returns quantity of recent block numbers seen by the client. From this perspective, method name is a bit confusing. What is your suggestion about changing to getblockNumbers()?
    opened by awebartisan 2
  • Cant load web3-php

    Cant load web3-php

    I am not an expert, i am having this trouble. First i created a composer.json:

    { "minimum-stability": "dev",

    }

    then, in visual code, from terminal, i wrote this command: "composer require web3-php/web3" and terminal thorw this:

    Using version dev-master for web3-php/web3 ./composer.json has been updated Running composer update web3-php/web3 Loading composer repositories with package information Updating dependencies Lock file operations: 12 installs, 0 updates, 0 removals

    • Locking guzzlehttp/guzzle (dev-master c1fd316)
    • Locking guzzlehttp/promises (dev-master fe752ae)
    • Locking guzzlehttp/psr7 (dev-master c5b547b)
    • Locking paragonie/constant_time_encoding (v2.5.0)
    • Locking paragonie/random_compat (v9.99.100)
    • Locking phpseclib/phpseclib (3.0.x-dev 60edff7)
    • Locking psr/http-client (dev-master 22b2ef5)
    • Locking psr/http-factory (dev-master 36fa03d)
    • Locking psr/http-message (dev-master efd67d1)
    • Locking ralouphie/getallheaders (3.0.3)
    • Locking symfony/deprecation-contracts (dev-main 893fd20)
    • Locking web3-php/web3 (dev-master 061828b) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 12 installs, 0 updates, 0 removals
    • Downloading psr/http-message (dev-master efd67d1)
    • Downloading psr/http-factory (dev-master 36fa03d)
    • Downloading guzzlehttp/psr7 (dev-master c5b547b)
    • Downloading psr/http-client (dev-master 22b2ef5)
    • Downloading symfony/deprecation-contracts (dev-main 893fd20)
    • Downloading phpseclib/phpseclib (3.0.x-dev 60edff7)
    • Downloading guzzlehttp/guzzle (dev-master c1fd316)
    • Installing guzzlehttp/promises (dev-master fe752ae): Extracting archive
    • Installing ralouphie/getallheaders (3.0.3): Extracting archive
    • Installing psr/http-message (dev-master efd67d1): Extracting archive
    • Installing psr/http-factory (dev-master 36fa03d): Extracting archive
    • Installing guzzlehttp/psr7 (dev-master c5b547b): Extracting archive
    • Installing paragonie/random_compat (v9.99.100): Extracting archive
    • Installing psr/http-client (dev-master 22b2ef5): Extracting archive
    • Installing symfony/deprecation-contracts (dev-main 893fd20): Extracting archive
    • Installing phpseclib/phpseclib (3.0.x-dev 60edff7): Extracting archive
    • Installing guzzlehttp/guzzle (dev-master c1fd316): Extracting archive
    • Installing web3-php/web3 (dev-master 061828b): Extracting archive 7 package suggestions were added by new dependencies, use composer suggest to see details. Generating autoload files 6 packages you are using are looking for funding. Use the composer fund command to find out more!

    And in folder, appear the composer.lock and vendor folder with a bunch of stuff installed. imagen

    imagen

    Inside the vendor folder there is an autoload.php file, which has this code in it:

    // autoload.php @generated by Composer

    require_once DIR . '/composer/autoload_real.php';

    return ComposerAutoloaderInit9f1c798e6bb2829771949001eeeee0c7::getLoader();

    so i want to make a test, and add the following to the autoload.php file:

    // autoload.php @generated by Composer

    require_once DIR . '/composer/autoload_real.php';

    return ComposerAutoloaderInit9f1c798e6bb2829771949001eeeee0c7::getLoader();

    print ("test");

    Then go to my browser, http://localhost/web3ppp/vendor/autoload.php and nothing happend: imagen

    but if i put the print ("test"); before the return line, the text "test" appear,

    // autoload.php @generated by Composer

    require_once DIR . '/composer/autoload_real.php';

    print ("test");

    return ComposerAutoloaderInit9f1c798e6bb2829771949001eeeee0c7::getLoader();

    imagen

    so there is something wrong with the return line, but i dont know what is wrong.

    Im using xampp with php 8.0.6 on windows 10.

    Can anyone tell me what im doing wrong?

    opened by jose5958 1
  • Invalid content type, only application/json is supported

    Invalid content type, only application/json is supported

    Platform : windows 10 Php : 8.0.13 Geth : 1.10.14-stable

    Trying to fetch eth accounts results in an invalid null $response passed to array_key_exists at src\Transporters\Http.php:55

    image

    Debuging the contents of the http response shows a string Invalid content type, only application/json is supported

    image

    Adding a content type to the request header fixes the issue

    image

    @nunomaduro Let me know if i can help fixing the issue and make a PR ! cheers,

    opened by lotfio 1
  • PULL_REQUEST_TEMPLATE.md is missing

    PULL_REQUEST_TEMPLATE.md is missing

    In

    https://github.com/web3-php/web3/blob/master/CONTRIBUTING.md#process

    1. is a link to a PR template

    This template is missing

    https://github.com/web3-php/web3/blob/master/.github/PULL_REQUEST_TEMPLATE.md

    opened by hammer-rot 1
  • feat: adds `isMining` to `Eth` namespace

    feat: adds `isMining` to `Eth` namespace

    Description

    • Adds isMining() method under Eth namespace

    Notes

    I would love to be able to help on this project. I was able to make this contribution by looking at following things:

    Can you please recommend some resources to look into for more effective contribution?

    Thanks!

    opened by awebartisan 1
  • Update README.md added dev-master

    Update README.md added dev-master

    added dev-master for resolve this error:

    [InvalidArgumentException]
      Could not find a version of package web3-php/web3 matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.
    
    opened by NabiKAZ 0
  • We are not sure. Feel free to pull request documentation or code that clarifies that.

    We are not sure. Feel free to pull request documentation or code that clarifies that.

    We are not sure. Feel free to pull request documentation or code that clarifies that.

    Originally posted by @nunomaduro in https://github.com/web3-php/web3/issues/24#issuecomment-1027874338

    I use this Python tutorial to understand which stream to decode. https://medium.com/coinmonks/unlocking-the-secrets-of-an-ethereum-transaction-3a33991f696c

    Also use some resources from this class https://github.com/CatAndPigeons/ethereum-adapter/blob/17b34c89976e1606904e64f97524280a8cf0ba50/src/Connector/EthereumRpcServiceTrait.php

    So I created a Trait with a decodeInput method to decode ERC20 or BEP20 transfer transaction.

    Thanks

    opened by frmiqueias 0
  • fixed: Invalid content type #22

    fixed: Invalid content type #22

    1

    Some clients always respond with invalid request headers, such as me and #22

    I have fixed this problem and it has worked for me.

    My client version is Geth/v1.10.14-stable-11a3a350/linux-amd64/go1.17.5

    opened by Xeath 0
  • How to get Token Balance ?

    How to get Token Balance ?

    i want to ERC20 token balance with your module;

    similar code for web3p;

    $contract = new Contract($web3->provider, $string); $contract->at($contractAddress)->call('balanceOf', $address, function ($err, $account) use (&$balance) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } $balance=goThen($account); });

    opened by mesutgunay 0
Owner
Web3 PHP
Web3 PHP gets dapp development more productive and enjoyable.
Web3 PHP
json-rpc client base on http

laravel JSON-RPC客户端(Http协议) 本项目是基于JSON-RPC的服务端端实现的rpc客户端 server端见:https://sajya.github.io/ 初始化 发布 php artisan vendor:publish --provider="Ze\JsonRpcCli

Zeee 2 Nov 19, 2021
json-rpc client base on http

laravel JSON-RPC客户端(Http协议) 本项目是基于JSON-RPC的服务端端实现的rpc客户端 server端见:https://sajya.github.io/ 初始化 1.发布 php artisan vendor:publish --provider="Ze\JsonRPCC

null 2 Nov 19, 2021
PHP 5.3+ library which helps you to interact with the DigitalOcean API

DigitalOcean The version 2 of the API will be available soon ! Please visit DigitalOceanV2 and contribute :) This PHP 5.3+ library helps you to intera

Antoine Kirk 156 Jul 30, 2022
A library written in PHP to interact with Coinbase Pro via API.

A library written in PHP to interact with Coinbase Pro via API.

Blake Hamilton 4 Mar 31, 2022
oursms.app client library that allows you to send SMS

Oursms laravel client https://oursms.app client library that allows you to send SMS Installation Install oursms client with composer composer requir

Khalid Mohammad 11 Aug 27, 2022
Google-api-php-client - A PHP client library for accessing Google APIs

Google APIs Client Library for PHP Reference Docs https://googleapis.github.io/google-api-php-client/main/ License Apache 2.0 The Google API Client Li

Google APIs 8.3k Dec 1, 2022
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via PHP

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via PHP

Twilio SendGrid 1.4k Nov 25, 2022
A2Reviews Client API lets you build apps, extensions or plugins to get reviews from the A2reviews APP

A2Reviews Client API lets you build apps, extensions or plugins to get reviews from the A2reviews APP. Including adding reviews to a store's products. It is used to import and export reviews through the API. This is the official package built and developed by A2Reviews, Inc.

Be Duc Tai 2 Sep 25, 2021
Nexmo REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Client Library for PHP Support Notice This library and it's associated packages, nexmo/client and nexmo/client-core have transitioned into a "Maintena

Nexmo 75 Sep 23, 2022
DigitalOcean API v2 client for Symfony and API Platform

DigitalOcean Bundle for Symfony and API Platform DunglasDigitalOceanBundle allows using the DigitalOcean API from your Symfony and API Platform projec

Kévin Dunglas 25 Jul 27, 2022
API client for ThePay - payment gate API

This is the official highly compatible public package of The Pay SDK which interacts with The Pay's REST API. To get started see examples below.

ThePay.cz s.r.o. 3 Oct 27, 2022
Code Quiz MonoRepo (API, API Client, App)

Code Quiz Welcome to the Code Quiz Open Source project from How To Code Well. This is an Open Source project that includes an API and an App for the d

How To Code Well 2 Nov 20, 2022
HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast usage.

HttpRequestor from Patienceman HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast

Manirabona Patience 2 Aug 26, 2022
A simple PHP GitHub API client, Object Oriented, tested and documented.

PHP GitHub API A simple Object Oriented wrapper for GitHub API, written with PHP. Uses GitHub API v3 & supports GitHub API v4. The object API (v3) is

KNP Labs 2k Nov 27, 2022
A simple Object Oriented PHP Client for Termii SMS API

Termii Client A simple Object Oriented PHP Client for Termii SMS API. Uses Termii API. Requirements PHP >= 7.2 Guzzlehttp ~6|~7 Installation Via Compo

Ilesanmi Olawale Adedotun 5 Feb 24, 2022
Xendit REST API Client for PHP - Card, Virtual Account, Invoice, Disbursement, Recurring Payments, Payout, EWallet, Balance, Retail Outlets Services

Xendit REST API Client for PHP - Card, Virtual Account, Invoice, Disbursement, Recurring Payments, Payout, EWallet, Balance, Retail Outlets Services

Xendit 92 Dec 4, 2022
php 8 client for the lemon.markets api

lemon.markets php client This repository contains a php 8+ compatible client for the https://lemon.markets API. The documentation of the API can be fo

Daniel Freudenberger 4 Nov 17, 2022
PHP client for Microsoft Azure Face API.

Microsoft Azure Face API PHP client A PHP library that utilizes Azure Face REST API. Requirements PHP >= 7.4 Installation composer require darmen/php-

Darmen Amanbayev 6 Sep 14, 2022
Google PHP API Client Services

Google PHP API Client Services

Google APIs 1.1k Dec 3, 2022