Search code examples
phpblockchainethereumsolidityweb3js

How to add smart contract functionality in PHP?


I'm working on project with main theme - sales. So, for front end (React) and Backend(PHP).

I had a problem understanding how to use my smart contract in the project. For deploy i use ethereum wallet 0.9.0 and test network Rinkeby. In EW he is working.

Let's pretend that I have button buy in my code, and, i have function in my contract like:

function Buy() 
    public 
    payable 
{
    require (msg.sender != seller);
    require (msg.value >= price);
    orderNum++;
    safePay +=msg.value;
}

Now, how i can use it? I heard about web3.js, but (i'm not sure) i need php requests for this.

Or, if my contract deploy in ethereum-wallet Rinkeby i can use it straight from just code and don't need deploy again.

I'm beginner in this theme and can do mistake (even in the view), but will be very glad, if someone can explain this.

Thanks.


Solution

  • AFAIK, there is no official support for PHP yet. You have 2 options here to interact with Ethereum blockchain:

    1. Use JSON RPC - using curl or any HTTP client library written in PHP, you can communicate with the Ethereum blockchain. Calling data is easy. You might face some difficulties when you want to write/edit data in Ethereum blockchain because you need to deal with transaction signing and packing the data/payload.

    2. Use Web3JS - This is much easier and you'll find many resources online. The keyword that you can use is building dapp. Truffle and this YouTube channel are two good places to learn about building dapps using Web3JS.