var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://blahblah.com:8545"));
var token = web3.eth.contract(ABI).at("x0x_contract_address")

How would this be done in PHP?

For example with the ethereum.php library I use I can do things like this:

$data = new Ethereum_Transaction($users_eth_address, "x0_eth_address, $gas, $gasPrice, $hex_balance);
$result = $ethereum->eth_sendTransaction($data);
  • Did you find an answer? – Malone Dec 10 '17 at 14:58
  • I basically had to convert all my PHP to js / node.js to get it to work using web3js. Then I run the node as a cron on a non web accessible page because of the personal unlockaccount password and the password to the RPC can't be visible to the public. – Marc Alexander Dec 11 '17 at 18:29
  • 1
    Sweet, it might be worth answering your own question then. I’d say people would be interested in the details of how you did that! – Malone Dec 11 '17 at 18:33

btelle/ethereum-php is incomplete and outdated. Last commit is from 2015.

You might use digitaldonkey/ethereum-php

// The json file "$fileName" used is what you get when you compile a contract with Truffle.

$ContractMeta = json_decode(file_get_contents($fileName));
$contract = new SmartContract(
  new Ethereum(SERVER_URL)
$someBytes = new EthBytes('34537ce3a455db6b')
$x = $contract->myContractMethod();
echo $x->val()

According to the repository

// include the class file
require 'ethereum.php';

// create a new object
$ethereum = new Ethereum('', 8545);

// do your thing
echo $ethereum->net_version();

This will allow you to connect to a local Geth instance on your machine listening on port 8545.

For accessing a contract please refer to the test.php file that includes more examples of how to make use of the functions available to you.

You may find the documentation for the JSON RPC API particularly useful here. This is because the ethereum.php project is essentially just a wrapper around the Ethereum implementation running at localhost:8545.

Hope this helps.

  • I'm already using this via my send transaction php example in my question post. I'm asking specifically for the php equivalent of var token = web3.eth.contract(ABI).at("x0x_contract_address") so I can access the token to transfer. I've tried $ethereum->eth_contract(ABI)->at('x0_contract address') – Marc Alexander Dec 5 '17 at 23:01
  • the at(address) function does not correspond to a particular JSON RPC API. Therefore you'll not see an equivalent function in the ethereum.php project. at(address) is called to get access to existing contract on a blockchain. Under the hood, when you create a new Contract. Web3 is just invoking standard JSON RPC functions to like eth_sendTransaction. You'll not get as "clean" of an api with the php wrapper like you do with Web3js. – Malone Dec 6 '17 at 12:41
  • This library is totally outdated. – digitaldonkey Jul 2 '18 at 10:20
  • At the time of answering this question it wasn’t. – Malone Jul 2 '18 at 10:28
  • It's outdated since 2015 ;) – digitaldonkey Sep 20 '18 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.