查看“PHP开发者指南”的源代码
←
PHP开发者指南
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于这些用户组的用户使用:trusted、truster、ninja
您必须确认您的电子邮件地址才能编辑页面。请通过
系统设置
设置并确认您的电子邮件地址。
您可以查看与复制此页面的源代码。
'''L'''inux '''A'''pache '''M'''ySQL '''P'''HP + Bitcoin 教程。 本教程假设运行 PHP 脚本的服务器为 Ubuntu,本例将让一个购物网站接受 Bitcoin. 我们假设你已经了解了一些 Bitcoin 的一些知识并有一定的 PHP 开发经验. 当然,你也可以将这里的 PHP 语言替换为你熟悉的其它语言,请访问[[API reference (JSON-RPC)|API参考手册 (JSON-RPC)]]了解更多。 请保持你的 Bitcoin 运行于服务器模式,PHP 通过本地 HTTP 请求与之进行通信,使用[http://jsonrpcphp.org/ JSON-RPC]库来运行完成功能,服务器将返回[http://en.wikipedia.org/wiki/Json JSON]格式数据。 == 配置 Bitcoin == 你需要 bitcoind 命令行程序,你可以自行编译码代码,也可以从 [http://www.bitcoin.org/ bitcoin.org] 下载二进制文件. 关于如何配置 bitcoin 请查看[[Running Bitcoin]]了解更多。 Before running bitcoind you will need to create a file in the bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux): <source lang="bash"> rpcuser=user rpcpassword={you MUST pick a unique password to be secure} </source> Now run bitcoind: <source lang="bash"> $ ./bitcoind # wait a few seconds for it to start up $ ./bitcoind getinfo # various info shown $ ./bitcoind help # help on commands </source> Bitcoin is now initialising and you must wait until "blocks" is at the [http://blockexplorer.com/q/getblockcount current count]. == First steps == 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. <source lang="php"> require_once 'jsonRPCClient.php'; $bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/'); echo "<pre>\n"; print_r($bitcoin->getinfo()); echo "</pre>"; </source> == 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 [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 == 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 explained|accounts]]. Each account holds multiple addresses and acts like a mini-Bitcoin. <source lang="bash"> $ ./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 </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]. 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: <source lang="php"> <?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>"); } ?> </source> This creates a new address at the beginning of every new session, and stores it in the session variable. ==See Also== * [[API reference (JSON-RPC)]] * [[Lazy_API]] * [[Merchant Howto]] [[Category:Developer]]
返回至
PHP开发者指南
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息