{"id":15031519,"url":"https://github.com/web3p/web3.php","last_synced_at":"2025-05-14T09:09:15.855Z","repository":{"id":37425401,"uuid":"111753905","full_name":"web3p/web3.php","owner":"web3p","description":"A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.","archived":false,"fork":false,"pushed_at":"2024-07-28T10:28:26.000Z","size":1015,"stargazers_count":1187,"open_issues_count":172,"forks_count":561,"subscribers_count":58,"default_branch":"master","last_synced_at":"2025-01-07T16:31:03.516Z","etag":null,"topics":["ethereum","hacktoberfest","php","smart-contracts","web3","web3php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/web3p.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-23T02:28:19.000Z","updated_at":"2025-01-04T12:42:10.000Z","dependencies_parsed_at":"2023-01-27T22:01:25.819Z","dependency_job_id":"35593cab-7525-43f2-bc99-bf3e98a960be","html_url":"https://github.com/web3p/web3.php","commit_stats":{"total_commits":398,"total_committers":18,"mean_commits":22.11111111111111,"dds":0.07537688442211055,"last_synced_commit":"cede0cc410363ec6182a732e98bb017e421352f4"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3p%2Fweb3.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3p%2Fweb3.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3p%2Fweb3.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3p%2Fweb3.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/web3p","download_url":"https://codeload.github.com/web3p/web3.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247962605,"owners_count":21024871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum","hacktoberfest","php","smart-contracts","web3","web3php"],"created_at":"2024-09-24T20:15:53.322Z","updated_at":"2025-04-09T02:18:07.912Z","avatar_url":"https://github.com/web3p.png","language":"PHP","readme":"# web3.php\n\n[![PHP](https://github.com/web3p/web3.php/actions/workflows/php.yml/badge.svg)](https://github.com/web3p/web3.php/actions/workflows/php.yml)\n[![Build Status](https://travis-ci.org/web3p/web3.php.svg?branch=master)](https://travis-ci.org/web3p/web3.php)\n[![codecov](https://codecov.io/gh/web3p/web3.php/branch/master/graph/badge.svg)](https://codecov.io/gh/web3p/web3.php)\n[![Join the chat at https://gitter.im/web3-php/web3.php](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg)](https://gitter.im/web3-php/web3.php)\n[![Licensed under the MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/web3p/web3.php/blob/master/LICENSE)\n\n\nA php interface for interacting with the Ethereum blockchain and ecosystem.\n\n# Install\n\nSet minimum stability to dev\n```\n\"minimum-stability\": \"dev\"\n```\n\nThen\n```\ncomposer require web3p/web3.php dev-master\n```\n\nOr you can add this line in composer.json\n\n```\n\"web3p/web3.php\": \"dev-master\"\n```\n\n\n# Usage\n\n### New instance\n```php\nuse Web3\\Web3;\n\n$web3 = new Web3('http://localhost:8545');\n```\n\n### Using provider\n```php\nuse Web3\\Web3;\nuse Web3\\Providers\\HttpProvider;\n\n$web3 = new Web3(new HttpProvider('http://localhost:8545'));\n\n// timeout\n$web3 = new Web3(new HttpProvider('http://localhost:8545', 0.1));\n```\n\n### You can use callback to each rpc call:\n```php\n$web3-\u003eclientVersion(function ($err, $version) {\n    if ($err !== null) {\n        // do something\n        return;\n    }\n    if (isset($version)) {\n        echo 'Client version: ' . $version;\n    }\n});\n```\n\n### Async\n```php\nuse Web3\\Web3;\nuse Web3\\Providers\\HttpAsyncProvider;\n\n$web3 = new Web3(new HttpAsyncProvider('http://localhost:8545'));\n\n// timeout\n$web3 = new Web3(new HttpAsyncProvider('http://localhost:8545', 0.1));\n\n// await\n$promise = $web3-\u003eclientVersion(function ($err, $version) {\n    // do somthing\n});\nAsync\\await($promise);\n```\n\n### Websocket\n```php\nuse Web3\\Web3;\nuse Web3\\Providers\\WsProvider;\n\n$web3 = new Web3(new WsProvider('ws://localhost:8545'));\n\n// timeout\n$web3 = new Web3(new WsProvider('ws://localhost:8545', 0.1));\n\n// await\n$promise = $web3-\u003eclientVersion(function ($err, $version) {\n    // do somthing\n});\nAsync\\await($promise);\n\n// close connection\n$web3-\u003eprovider-\u003eclose();\n```\n\n### Eth\n```php\nuse Web3\\Web3;\n\n$web3 = new Web3('http://localhost:8545');\n$eth = $web3-\u003eeth;\n```\n\nOr\n\n```php\nuse Web3\\Eth;\n\n$eth = new Eth('http://localhost:8545');\n```\n\n### Net\n```php\nuse Web3\\Web3;\n\n$web3 = new Web3('http://localhost:8545');\n$net = $web3-\u003enet;\n```\n\nOr\n\n```php\nuse Web3\\Net;\n\n$net = new Net('http://localhost:8545');\n```\n\n### Batch\n\nweb3\n```php\n$web3-\u003ebatch(true);\n$web3-\u003eclientVersion();\n$web3-\u003ehash('0x1234');\n$web3-\u003eexecute(function ($err, $data) {\n    if ($err !== null) {\n        // do something\n        // it may throw exception or array of exception depends on error type\n        // connection error: throw exception\n        // json rpc error: array of exception\n        return;\n    }\n    // do something\n});\n```\n\neth\n\n```php\n$eth-\u003ebatch(true);\n$eth-\u003eprotocolVersion();\n$eth-\u003esyncing();\n\n$eth-\u003eprovider-\u003eexecute(function ($err, $data) {\n    if ($err !== null) {\n        // do something\n        return;\n    }\n    // do something\n});\n```\n\nnet\n```php\n$net-\u003ebatch(true);\n$net-\u003eversion();\n$net-\u003elistening();\n\n$net-\u003eprovider-\u003eexecute(function ($err, $data) {\n    if ($err !== null) {\n        // do something\n        return;\n    }\n    // do something\n});\n```\n\npersonal\n```php\n$personal-\u003ebatch(true);\n$personal-\u003elistAccounts();\n$personal-\u003enewAccount('123456');\n\n$personal-\u003eprovider-\u003eexecute(function ($err, $data) {\n    if ($err !== null) {\n        // do something\n        return;\n    }\n    // do something\n});\n```\n\n### Contract\n\n```php\nuse Web3\\Contract;\n\n$contract = new Contract('http://localhost:8545', $abi);\n\n// deploy contract\n$contract-\u003ebytecode($bytecode)-\u003enew($params, $callback);\n\n// call contract function\n$contract-\u003eat($contractAddress)-\u003ecall($functionName, $params, $callback);\n\n// change function state\n$contract-\u003eat($contractAddress)-\u003esend($functionName, $params, $callback);\n\n// estimate deploy contract gas\n$contract-\u003ebytecode($bytecode)-\u003eestimateGas($params, $callback);\n\n// estimate function gas\n$contract-\u003eat($contractAddress)-\u003eestimateGas($functionName, $params, $callback);\n\n// get constructor data\n$constructorData = $contract-\u003ebytecode($bytecode)-\u003egetData($params);\n\n// get function data\n$functionData = $contract-\u003eat($contractAddress)-\u003egetData($functionName, $params);\n```\n\n# Assign value to outside scope(from callback scope to outside scope)\nDue to callback is not like javascript callback, \nif we need to assign value to outside scope, \nwe need to assign reference to callback.\n```php\n$newAccount = '';\n\n$web3-\u003epersonal-\u003enewAccount('123456', function ($err, $account) use (\u0026$newAccount) {\n    if ($err !== null) {\n        echo 'Error: ' . $err-\u003egetMessage();\n        return;\n    }\n    $newAccount = $account;\n    echo 'New account: ' . $account . PHP_EOL;\n});\n```\n\n# Examples\n\nTo run examples, you need to run ethereum blockchain local (testrpc).\n\nIf you are using docker as development machain, you can try [ethdock](https://github.com/sc0vu/ethdock) to run local ethereum blockchain, just simply run `docker-compose up -d testrpc` and expose the `8545` port.\n\n# Develop\n\n### Local php cli installed\n\n1. Clone the repo and install packages.\n```\ngit clone https://github.com/web3p/web3.php.git \u0026\u0026 cd web3.php \u0026\u0026 composer install\n```\n\n2. Run test script.\n```\nvendor/bin/phpunit\n```\n\n### Docker container\n\n1. Clone the repo and run docker container.\n```\ngit clone https://github.com/web3p/web3.php.git\n```\n\n2. Copy web3.php to web3.php/docker/app directory and start container.\n```\ncp files docker/app \u0026\u0026 docker-compose up -d php ganache\n```\n\n3. Enter php container and install packages.\n```\ndocker-compose exec php ash\n```\n\n4. Change testHost in `TestCase.php`\n```\n/**\n * testHost\n * \n * @var string\n */\nprotected $testHost = 'http://ganache:8545';\n```\n\n5. Run test script\n```\nvendor/bin/phpunit\n```\n\n###### Install packages\nEnter container first\n```\ndocker-compose exec php ash\n```\n\n1. gmp\n```\napk add gmp-dev\ndocker-php-ext-install gmp\n```\n\n2. bcmath\n```\ndocker-php-ext-install bcmath\n```\n\n###### Remove extension\nMove the extension config from `/usr/local/etc/php/conf.d/`\n```\nmv /usr/local/etc/php/conf.d/extension-config-name to/directory\n```\n\n# API\n\nTodo.\n\n# Contribution\n\nThank you to all the people who already contributed to web3.php!\n\u003ca href=\"https://github.com/web3p/web3.php/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=web3p/web3.php\" /\u003e\n\u003c/a\u003e\n\n# License\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb3p%2Fweb3.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweb3p%2Fweb3.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb3p%2Fweb3.php/lists"}