PHP developer intro

For this introduction we assume that you have GNU/Linux server with Apache and PHP and that you wish to interact with the Bitcoin network from a web application. We assume some knowledge of Bitcoin and experience in PHP.

Linux Apache MySQL PHP + Bitcoin tutorial.

While this is written for PHP, the same principles apply for other languages. See the associated API reference pages for info on other languages.

The easiest way to get started is to run Bitcoin in daemon mode with which PHP communicates via local HTTP requests. A library called JSON-RPC is used to call the various functions of bitcoind, which will respond back with a JSON object.

It is however recommended to use one of the Alternative Libraries listed below instead, since they are more sophisticated.

Setting up Bitcoin

You can download the Bitcoin daemon from the homepage and run one of the included binaries or compile your own from the included source code. See Running Bitcoin for details on configuring bitcoind.

Before running bitcoind you will need to create a configuration file in the Bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):

rpcuser=user
rpcpassword={you MUST pick a unique password to be secure}

If you miss this step, bitcoind will remind you.

Now run bitcoind:

$ ./bitcoind
# wait a few seconds for it to start up
$ ./bitcoin-cli getblockchaininfo
# various information will be shown. If you get an error, try again until you see some useful output.
$ ./bitcoin-cli help
# get help on commands, note no dash before help

Bitcoin will begin synchronizing with the network and downloading a complete copy of the block chain. As of August 2012, more than 2gb of data must be downloaded and verified during this process. It may take two or more hours to complete. You will know when it's done when the block count reaches the current count.

Getblockchaininfo (Bitcoind's version of Hello World)

Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from JSON-RPC PHP and place it in a web-accessible location.

Second, create a PHP file with the following and visit it with your browser to test.

  require_once 'jsonRPCClient.php';
  
  $bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/');
   
  echo "<pre>\n";
  print_r($bitcoin->getblockchaininfo());
  echo "</pre>";

Note: The jsonRPCClient library uses fopen() and will throw an exception saying "Unable to connect" if it receives a 404 or 500 error from bitcoind. This prevents you from being able to see error messages generated by bitcoind (as they are sent with status 404 or 500). The Alternative Libraries listed below are similar in function to JSON-RPC PHP but do not have this issue.

Precision

Bitcoin amounts can range from 1 Satoshi (0.00000001 BTC) to nearly 2,100,000,000,000,000 (21,000,000 BTC). To avoid rounding errors, you must make sure your PHP implementation supports the full range of Bitcoin values without losing precision. Most PHP implementations use IEEE 64-bit double-precision floating point numbers with 53 bits of precision, which is enough to correctly represent the full range of bitcoin values.

See Proper Money Handling (JSON-RPC) for more information.

If your PHP implementation does not support 64-bit numbers (again, this is very rare), you must use a version of bitcoind that sends values as strings (genjix maintains a fork at http://github.com/genjix/bitcoin) and use the GMP and BC Math libraries for all calculations involving bitcoin amounts.

Accounts

In Bitcoin, money is sent to addresses and many addresses can be held by one wallet. The balance shown by default in bitcoind is the sum of the bitcoins in all the addresses in the wallet.

Bitcoin goes another step. You can have accounts. Each account holds multiple addresses and acts like a mini-bitcoind.

$ ./bitcoin-cli listaccounts
# show list of accounts and various info for each one
$ ./bitcoin-cli getaccountaddress user889
# get an address to receive money to that is unique for the account user889
$ ./bitcoin-cli getbalance user889
# get the sum of all the money in the addresses owned by the account user889

In your application, each user should have a unique username. You may then query bitcoind for a unique address using $bitcoin->getaccountaddress("user889"); [gets the first address for user889] or $bitcoin->getnewaddress("user889"); [creates a new address for user889].

The customer then deposits to this address.

You can check the funds for that customer by doing $bitcoin->getbalance("user889", 4);. The 4 indicates the minimum number of confirmations we will accept before assuming this payment is valid.

If you will be using accounts for multiple deposits and withdrawals long-term, you may want to consider tracking user balances in your own database. This simplifies transfers between your application's accounts and decouples your accounts from the Bitcoin wallet.

getnewaddress vs getaccountaddress

Using getnewaddress helps increase maintain anonymity of your users by making it hard for a malicious agent to track payments flowing through your application. Running getnewaddress too often, however, will cause your wallet to become filled with many empty addresses.

It is therefore recommended to in some way limit the number of unfunded addresses each user can request. Here is an example using sessions:

<?php
    require_once('jsonRPCClient.php');
    $bitcoin = new jsonRPCClient('http://root:root@127.0.0.1:8332/'); 
    # now check for appropriate funds in user account
    try {
        $username = ...
        if(isset($_SESSION['sendaddress']))
            $sendaddress = $_SESSION['sendaddress'];
        else {
            $sendaddress = $bitcoin->getnewaddress($username);
            $_SESSION['sendaddress'] = $sendaddress;
        }
        $balance = $bitcoin->getbalance($username);
    }
    catch (Exception $e) {
        die("<p>Server error! Please contact the admin.</p>");
    }
?>

This creates a new address at the beginning of every new session, and stores it in the session variable.

Alternative Libs For RPC

There are alternative PHP libraries for connecting to the bitcoind RPC which are recommended over using the plain jsonRPCClient.php class. They do not rely on magic __call, use cURL instead of fopen and have better error handling (and can be installed using composer).

Alternative CLI clients

  • jsonrpc-cli provides simple jsonrpc queries from the command-line with highlighted json results (colors) and the ability to view/debug raw http requests and responses. It can also display json results in php print_r, var_dump, serialize formats as well as yaml.

See Also

This article is issued from Bitcoin. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.