Bitcoin & Blockchain Programming #005 – RPCs via PHP

by Albert Szmigielski

It’s time to talk to bitcoind programmatically via RPC. Actually in our last tutorial, the bitcoin-cli calls were made over RPC as well (bitcoin-cli handles all the details). In today’s tutorial we will make RPCs using php. We are assuming you have php installed on your machine, if not you can do that by issuing the following command in a terminal:

If you are not sure if php is installed on your system you can check by doing:

The RPCs to bitcoind are accomplished via json format, so we will need a library for that: jsonRPCClient.php
We have downloaded it from
We need to create an object to connect to bitcoind, we will call it $bitcoin in our php program.
As arguments jsonRPCClient requires a username, a password, the IP and the port number where the bitcoind wallet is running.
The user name and password are the credentials you specified in the bitcoin.conf file for rpcuser and rpcpassword.
The IP is the IP of the machine where bitcoind is running, if you are running it on your own machine it will be We will also need the port number, which in case of testnet is 18322.

In our case, our credentials are

rpcuser: bitcoin_user
rpcpassword: qwertyu873nld9j4n09sn3mm
port: 18332

the format is as follows:

Putting it all together we have:

Let’s simply ask bitcoind for the block count. We could do something like the following:

and then just print it out:

As you can see the call $bitcoin->getblockcount() makes the RPC to bitcoind and returns the information in the variable we assign it to (in our case $blockcount). We can issue any bitcoind command in a similar fashion.

Let’s put it all together.

We can try other commands, let’s do getinfo

and then print it, just one piece of information say, the version

or we can iterate through the array and print it all out

Putting it all in a file:

Now, let’s run it

Screenshot - 16-02-03 - 01:28:04 PM

So there we have it. We can now connect to bitcoind programmatically, issue commands and receive information back. This will allow us to build more complex and sophisticated programs in the future.
In the next tutorial we will use python to accomplish the same objectives.

Leave a Reply

Your email address will not be published. Required fields are marked *