<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Nixle</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Nixle"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Nixle"/>
	<updated>2026-04-10T22:20:46Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=9896</id>
		<title>PHP developer intro</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=9896"/>
		<updated>2011-06-06T15:28:13Z</updated>

		<summary type="html">&lt;p&gt;Nixle: /* getnewaddress vs getaccountaddress */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;L&#039;&#039;&#039;inux &#039;&#039;&#039;A&#039;&#039;&#039;pache &#039;&#039;&#039;M&#039;&#039;&#039;ySQL &#039;&#039;&#039;P&#039;&#039;&#039;HP + Bitcoin tutorial.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
You can substitute any other language here for PHP. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
&lt;br /&gt;
== Setting up Bitcoin ==&lt;br /&gt;
&lt;br /&gt;
You need the bitcoind command-line daemon.  You can either compile it from source or download a binary from the bitcoin.org homepage.&lt;br /&gt;
&lt;br /&gt;
See [[Running Bitcoin]] for details on configuring bitcoin.&lt;br /&gt;
&lt;br /&gt;
Before running bitcoind you will need to create a file in the bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rpcuser=user&lt;br /&gt;
rpcpassword={you MUST pick a unique password to be secure}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now run bitcoind:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind&lt;br /&gt;
# wait a few seconds for it to start up&lt;br /&gt;
$ ./bitcoind getinfo&lt;br /&gt;
# various info shown&lt;br /&gt;
$ ./bitcoind help&lt;br /&gt;
# help on commands&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bitcoin is now initialising and you must wait until &amp;quot;blocks&amp;quot; is at the [http://bitcoinwatch.com/ current count].&lt;br /&gt;
&lt;br /&gt;
== First steps ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  require_once &#039;jsonRPCClient.php&#039;;&lt;br /&gt;
  &lt;br /&gt;
  $bitcoin = new jsonRPCClient(&#039;http://user:password@127.0.0.1:8332/&#039;);&lt;br /&gt;
   &lt;br /&gt;
  echo &amp;quot;&amp;lt;pre&amp;gt;\n&amp;quot;;&lt;br /&gt;
  print_r($bitcoin-&amp;gt;getinfo());&lt;br /&gt;
  echo &amp;quot;&amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Precision ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
See [[Proper Money Handling (JSON-RPC)]] for more information.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Accounts ==&lt;br /&gt;
&lt;br /&gt;
In Bitcoin, money is sent to addresses. Your balance is the total of all the money in all the address in your wallet.&lt;br /&gt;
&lt;br /&gt;
Bitcoin goes another step. You can have [[Accounts explained|accounts]]. Each account holds multiple addresses and acts like a mini-Bitcoin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind listaccounts&lt;br /&gt;
# show list of accounts and various info for each one&lt;br /&gt;
$ ./bitcoind getaccountaddress user889&lt;br /&gt;
# get an address to receive money to that is unique for the account user889&lt;br /&gt;
$ ./bitcoind getbalance user889&lt;br /&gt;
# get the sum of all the money in the addresses owned by the account user889&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your shopping system, each user should have a unique username. You then query bitcoin for a unique address using $bitcoin-&amp;gt;getaccountaddress(&amp;quot;user889&amp;quot;); [gets the first address for user889] or $bitcoin-&amp;gt;getnewaddress(&amp;quot;user889&amp;quot;); [creates a new address for user889].&lt;br /&gt;
&lt;br /&gt;
The customer then deposits to this address.&lt;br /&gt;
&lt;br /&gt;
You can check the funds for that customer by doing $bitcoin-&amp;gt;getbalance(&amp;quot;user889&amp;quot;, 4);. The 4 indicates the minimum number of confirmations we will accept before assuming this payment is valid.&lt;br /&gt;
&lt;br /&gt;
=== getnewaddress vs getaccountaddress ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
I recommend that you do something like:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    require_once(&#039;jsonRPCClient.php&#039;);&lt;br /&gt;
    $bitcoin = new jsonRPCClient(&#039;http://root:root@127.0.0.1:8332/&#039;); &lt;br /&gt;
    # now check for appropriate funds in user account&lt;br /&gt;
    try {&lt;br /&gt;
        $username = ...&lt;br /&gt;
        if(isset($_SESSION[&#039;sendaddress&#039;]))&lt;br /&gt;
            $sendaddress = $_SESSION[&#039;sendaddress&#039;];&lt;br /&gt;
        else {&lt;br /&gt;
            $sendaddress = $bitcoin-&amp;gt;getnewaddress($username);&lt;br /&gt;
            $_SESSION[&#039;sendaddress&#039;] = $sendaddress;&lt;br /&gt;
        }&lt;br /&gt;
        $balance = $bitcoin-&amp;gt;getbalance($username);&lt;br /&gt;
    }&lt;br /&gt;
    catch (Exception $e) {&lt;br /&gt;
        die(&amp;quot;&amp;lt;p&amp;gt;Server error! Please contact the admin.&amp;lt;/p&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a new address at the beginning of every new session, and stores it in the session variable.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[API reference (JSON-RPC)]]&lt;br /&gt;
* [[Lazy_API]]&lt;br /&gt;
* [[Merchant Howto]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Nixle</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=9894</id>
		<title>PHP developer intro</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=9894"/>
		<updated>2011-06-06T15:20:06Z</updated>

		<summary type="html">&lt;p&gt;Nixle: /* Accounts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;L&#039;&#039;&#039;inux &#039;&#039;&#039;A&#039;&#039;&#039;pache &#039;&#039;&#039;M&#039;&#039;&#039;ySQL &#039;&#039;&#039;P&#039;&#039;&#039;HP + Bitcoin tutorial.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
You can substitute any other language here for PHP. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
&lt;br /&gt;
== Setting up Bitcoin ==&lt;br /&gt;
&lt;br /&gt;
You need the bitcoind command-line daemon.  You can either compile it from source or download a binary from the bitcoin.org homepage.&lt;br /&gt;
&lt;br /&gt;
See [[Running Bitcoin]] for details on configuring bitcoin.&lt;br /&gt;
&lt;br /&gt;
Before running bitcoind you will need to create a file in the bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rpcuser=user&lt;br /&gt;
rpcpassword={you MUST pick a unique password to be secure}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now run bitcoind:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind&lt;br /&gt;
# wait a few seconds for it to start up&lt;br /&gt;
$ ./bitcoind getinfo&lt;br /&gt;
# various info shown&lt;br /&gt;
$ ./bitcoind help&lt;br /&gt;
# help on commands&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bitcoin is now initialising and you must wait until &amp;quot;blocks&amp;quot; is at the [http://bitcoinwatch.com/ current count].&lt;br /&gt;
&lt;br /&gt;
== First steps ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  require_once &#039;jsonRPCClient.php&#039;;&lt;br /&gt;
  &lt;br /&gt;
  $bitcoin = new jsonRPCClient(&#039;http://user:password@127.0.0.1:8332/&#039;);&lt;br /&gt;
   &lt;br /&gt;
  echo &amp;quot;&amp;lt;pre&amp;gt;\n&amp;quot;;&lt;br /&gt;
  print_r($bitcoin-&amp;gt;getinfo());&lt;br /&gt;
  echo &amp;quot;&amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Precision ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
See [[Proper Money Handling (JSON-RPC)]] for more information.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Accounts ==&lt;br /&gt;
&lt;br /&gt;
In Bitcoin, money is sent to addresses. Your balance is the total of all the money in all the address in your wallet.&lt;br /&gt;
&lt;br /&gt;
Bitcoin goes another step. You can have [[Accounts explained|accounts]]. Each account holds multiple addresses and acts like a mini-Bitcoin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind listaccounts&lt;br /&gt;
# show list of accounts and various info for each one&lt;br /&gt;
$ ./bitcoind getaccountaddress user889&lt;br /&gt;
# get an address to receive money to that is unique for the account user889&lt;br /&gt;
$ ./bitcoind getbalance user889&lt;br /&gt;
# get the sum of all the money in the addresses owned by the account user889&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your shopping system, each user should have a unique username. You then query bitcoin for a unique address using $bitcoin-&amp;gt;getaccountaddress(&amp;quot;user889&amp;quot;); [gets the first address for user889] or $bitcoin-&amp;gt;getnewaddress(&amp;quot;user889&amp;quot;); [creates a new address for user889].&lt;br /&gt;
&lt;br /&gt;
The customer then deposits to this address.&lt;br /&gt;
&lt;br /&gt;
You can check the funds for that customer by doing $bitcoin-&amp;gt;getbalance(&amp;quot;user889&amp;quot;, 4);. The 4 indicates the minimum number of confirmations we will accept before assuming this payment is valid.&lt;br /&gt;
&lt;br /&gt;
=== getnewaddress vs getaccountaddress ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
I recommend that you run do something like:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    require_once(&#039;jsonRPCClient.php&#039;);&lt;br /&gt;
    $bitcoin = new jsonRPCClient(&#039;http://root:root@127.0.0.1:8332/&#039;); &lt;br /&gt;
    # now check for appropriate funds in user account&lt;br /&gt;
    try {&lt;br /&gt;
        $username = ...&lt;br /&gt;
        if(isset($_SESSION[&#039;sendaddress&#039;]))&lt;br /&gt;
            $sendaddress = $_SESSION[&#039;sendaddress&#039;];&lt;br /&gt;
        else {&lt;br /&gt;
            $sendaddress = $bitcoin-&amp;gt;getnewaddress($username);&lt;br /&gt;
            $_SESSION[&#039;sendaddress&#039;] = $sendaddress;&lt;br /&gt;
        }&lt;br /&gt;
        $balance = $bitcoin-&amp;gt;getbalance($username);&lt;br /&gt;
    }&lt;br /&gt;
    catch (Exception $e) {&lt;br /&gt;
        die(&amp;quot;&amp;lt;p&amp;gt;Server error! Please contact the admin.&amp;lt;/p&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a new address at the beginning of every new session, and stores it in the session variable.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[API reference (JSON-RPC)]]&lt;br /&gt;
* [[Lazy_API]]&lt;br /&gt;
* [[Merchant Howto]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Nixle</name></author>
	</entry>
</feed>