PHP developer intro: Difference between revisions

From Bitcoin Wiki
Jump to navigation Jump to search
m ~/.bitcoin/.bitcoin.conf => ~/.bitcoin/bitcoin.conf
Belcher (talk | contribs)
Removed out-of-date getinfo method and replaced with getblockchaininfo
 
(21 intermediate revisions by 15 users not shown)
Line 1: Line 1:
'''L'''inux '''A'''pache '''M'''ySQL '''P'''HP + Bitcoin tutorial.
'''L'''inux '''A'''pache '''M'''ySQL '''P'''HP + Bitcoin tutorial.


For the sake of this tutorial we assume an Ubuntu server running with PHP. The use case here is integrating a shopping system to accept Bitcoins. We assume some knowledge of Bitcoin and experience in PHP.
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.


You can substitute any other language here for PHP. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.
While this is written for PHP, the same principles apply for other languages. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.


You will run Bitcoin in daemon mode. The way PHP communicates is through localhost HTTP requests. You use a library called [http://jsonrpcphp.org/ JSON-RPC] to call the various functions. It will respond back with a [http://en.wikipedia.org/wiki/Json JSON object].
The easiest way to get started is to run Bitcoin in daemon mode with which PHP communicates via local HTTP requests. A library called [http://jsonrpcphp.org/ JSON-RPC] is used to call the various functions of bitcoind, which will respond back with a [http://en.wikipedia.org/wiki/Json JSON object].
 
It is however recommended to use one of the [[#Alternative_Libs_For_RPC|Alternative Libraries]] listed below instead, since they are more sophisticated.


== Setting up Bitcoin ==
== Setting up Bitcoin ==


You need the bitcoind command-line daemon.  You can either compile it from source or download a binary from the bitcoin.org homepage.
You can download the Bitcoin daemon from the [[Main_Page|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.


See [[Running Bitcoin]] for details on configuring bitcoin.
Before running bitcoind you will need to create a configuration file in the Bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):
 
Before running bitcoind you will need to create a file in the bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):
<source lang="bash">
<source lang="bash">
rpcuser=user
rpcuser=user
rpcpassword={you MUST pick a unique password to be secure}
rpcpassword={you MUST pick a unique password to be secure}
</source>
</source>
If you miss this step, bitcoind will remind you.


Now run bitcoind:
Now run bitcoind:
Line 23: Line 24:
$ ./bitcoind
$ ./bitcoind
# wait a few seconds for it to start up
# wait a few seconds for it to start up
$ ./bitcoind getinfo
$ ./bitcoin-cli getblockchaininfo
# various info shown
# various information will be shown. If you get an error, try again until you see some useful output.
$ ./bitcoind help
$ ./bitcoin-cli help
# help on commands
# get help on commands, note no dash before help
</source>
</source>


Bitcoin is now initialising and you must wait until "blocks" is at the [http://bitcoinwatch.com/ current count].
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 [http://blockexplorer.com/q/getblockcount current count].


== First steps ==
== Getblockchaininfo (Bitcoind's version of Hello World) ==


Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from [http://jsonrpcphp.org/ JSON-RPC PHP]. The other files can be safely discarded.
Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from [http://jsonrpcphp.org/ 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.


<source lang="php">
<source lang="php">
Line 41: Line 44:
    
    
   echo "<pre>\n";
   echo "<pre>\n";
   print_r($bitcoin->getinfo());
   print_r($bitcoin->getblockchaininfo());
   echo "</pre>";
   echo "</pre>";
</source>
</source>
'''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_Libs_For_RPC|Alternative Libraries]] listed below are similar in function to JSON-RPC PHP but do not have this issue.


== Precision ==
== Precision ==


Bitcoin amounts can range from 1 (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, which have 53 bits of precision, which is enough to correctly represent the full range of bitcoin values.
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.
See [[Proper Money Handling (JSON-RPC)]] for more information.


If your PHP implementation does not support 64-bit numbers, 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  [http://php.net/manual/en/ref.gmp.php GMP] and [http://php.net/manual/en/ref.bc.php BC Math] libraries for all calculations involving bitcoin amounts.
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  [http://php.net/manual/en/ref.gmp.php GMP] and [http://php.net/manual/en/ref.bc.php BC Math] libraries for all calculations involving bitcoin amounts.


== Accounts ==
== Accounts ==


In Bitcoin, money is sent to addresses. Your balance is the total of all the money in all the address in your wallet.
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 explained|accounts]]. Each account holds multiple addresses and adds like a mini-Bitcoin.
Bitcoin goes another step. You can have [[Accounts explained|accounts]]. Each account holds multiple addresses and acts like a mini-bitcoind.  


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


In your shopping system, each user should have a unique username. You then query bitcoin for a unique address using $bitcoin->getaccountaddress("user889"); [gets the first address for user889] or $bitcoin->getnewaddress("user889"); [creates a new address for 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.
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.
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 ===
=== getnewaddress vs getaccountaddress ===


Using getnewaddress helps increase the anonymity of your customers by making it hard to track their payments from the POV of a malicious agent. However running it too often will cause your wallet to become filled with many empty addresses.
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.


I recommend that you run do something like:
It is therefore recommended to in some way limit the number of unfunded addresses each user can request. Here is an example using sessions:
<source lang="php">
<source lang="php">
<?php
<?php
Line 101: Line 108:


This creates a new address at the beginning of every new session, and stores it in the session variable.
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).
* [https://github.com/nbobtc/bitcoind-php NboBTC Bitcoind-PHP]
* [https://github.com/aceat64/EasyBitcoin-PHP EasyBitcoin-PHP]
==Alternative CLI clients==
* [https://github.com/dan-da/jsonrpc-cli 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==
==See Also==
Line 106: Line 124:
* [[API reference (JSON-RPC)]]
* [[API reference (JSON-RPC)]]
* [[Lazy_API]]
* [[Lazy_API]]
* [https://github.com/cryptoapi/Payment-Gateway Bitcoin-PHP Payment library]
* [[Merchant Howto]]
* [[Merchant Howto]]
* [[https://github.com/Bit-Wasp/bitcoin-lib-php Bitcoin-Lib-PHP - PHP Lib implementing signing of transactions, BIP32, etc]]
[[es:Introducción para desarrolladores de PHP]]
[[de:Einführung_für_PHP-Entwickler]]


[[Category:Developer]]
[[Category:Developer]]

Latest revision as of 21:12, 25 February 2020

Linux Apache MySQL PHP + Bitcoin tutorial.

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.

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