PHP开发者指南

来自Bitcoin Wiki
跳转至: 导航搜索

Linux Apache MySQL PHP + Bitcoin 教程。

本教程假设运行 PHP 脚本的服务器为 Ubuntu,本例将让一个购物网站接受 Bitcoin. 我们假设你已经了解了一些 Bitcoin 的一些知识并有一定的 PHP 开发经验.

当然,你也可以将这里的 PHP 语言替换为你熟悉的其它语言,请访问API参考手册 (JSON-RPC)了解更多。

请保持你的 Bitcoin 运行于服务器模式,PHP 通过本地 HTTP 请求与之进行通信,使用JSON-RPC库来运行完成功能,服务器将返回JSON格式数据。

配置 Bitcoin

你需要 bitcoind 命令行程序,你可以自行编译码代码,也可以从 bitcoin.org 下载二进制文件.

关于如何配置 bitcoin 请查看Running Bitcoin了解更多。

运行 bitcoind 之前需要在 bitcoin 数据文件夹中新建一个配置文件(Linux 系统位于~/.bitcoin/bitcoin.conf,Windows 位于%APPDATA%\Bitcoin\)包括以下内容:

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

然后运行 bitcoind,我们建议你先泡杯茶:

$ ./bitcoind
# 启动有点慢,要有耐心
$ ./bitcoind getinfo
# 显示有关信息,various info shown
$ ./bitcoind help
# 帮助,help on commands

Bitcoin 就初始化成功了,你现在要做的就是慢慢品茶,等着下载“货币包(blocks)”,查看现有货币包.

First steps

Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from JSON-RPC PHP. The other files can be safely discarded.

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

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.

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. Your balance is the total of all the money in all the address in your wallet.

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

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

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].

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.

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.

I recommend that you do something like:

<?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.

See Also