matmar10 / coinbase-php
Coinbase API for PHP >=5.3.3
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-09-24 02:38:04 UTC
README
*注意:这是一个直接从 https://github.com/coinbase/coinbase-php 分支出来的,支持命名空间和Psr0自动加载* *
通过Coinbase API轻松购买、发送和接受比特币。
此库支持API密钥认证方法和OAuth。以下示例使用API密钥 - 关于如何使用OAuth的说明,请参阅OAuth认证。
安装
使用以下命令获取Coinbase PHP库的最新版本
git clone https://github.com/coinbase/coinbase-php
然后,将以下内容添加到您的PHP脚本中
require_once("/path/to/coinbase-php/lib/Coinbase.php");
使用方法
首先,在您的账户上启用API密钥。
接下来,创建客户端实例,并将您的API密钥作为第一个(也是唯一一个)参数传递。
$coinbase = new Coinbase($_ENV['COINBASE_API_KEY'])
请注意,我们没有将API密钥硬编码到我们的代码库中,而是将其设置在环境变量中。这只是其中一个例子,但保持您的凭据与代码库分离是良好的安全实践。
现在您可以在$coinbase
上调用类似API参考中描述的方法。例如
$balance = $coinbase->getBalance(); echo "Balance is " . $balance . " BTC";
货币金额以字符串形式返回。为了避免精度错误,请使用PHP任意精度数学函数处理货币金额。
示例
检查您的余额
echo $coinbase->getBalance() . " BTC"; // '200.123 BTC'
发送比特币
public function sendMoney($to, $amount, $notes=null, $userFee=null, $amountCurrency=null)
$response = $coinbase->sendMoney("user@example.com", "2"); echo $response->success ? 'true' : 'false'; // 'true' echo $response->transaction->status; // 'pending' echo $response->transaction->id; // '518d8567ed3ddcd4fd000034'
第一个参数也可以是比特币地址,第三个参数可以是交易笔记或描述。描述仅在Coinbase上可见(不在比特币网络上)。
$response = $coinbase->sendMoney("mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x", "0.1", "thanks for the coffee!"); echo $response->transaction->notes; // 'thanks for the coffee!'
您还可以使用第五个参数以多种货币发送货币(请参阅getCurrencies()
)。金额将自动根据当前汇率转换为正确的BTC金额。
$response = $coinbase->sendMoney("user@example.com", "2", null, null, "CAD"); echo $response->transaction->amount->amount; // '0.0169'
请求比特币
这将向收件人发送一封电子邮件,请求付款,并提供一种简单的付款方式。
$response = $coinbase->requestMoney('client@example.com', 50, "contractor hours in January (website redesign for 50 BTC)"); echo $response->transaction->request ? 'true' : 'false'; // 'true' echo $response->transaction->id; // '501a3554f8182b2754000003' $response = $coinbase->resendRequest('501a3554f8182b2754000003'); echo $response->success ? 'true' : 'false'; // 'true' $response = $coinbase->cancelRequest('501a3554f8182b2754000003'); echo $response->success ? 'true' : 'false'; // 'true' // From the other account: $response = $coinbase->completeRequest('501a3554f8182b2754000003'); echo $response->success ? 'true' : 'false'; // 'true'
列出您当前的交易
按时间戳降序排序,每页30条。您可以通过传递一个整数作为第一个参数来浏览结果,例如$coinbase->getTransactions(2)
。
$response = $coinbase->getTransactions(); echo $response->current_page; // '1' echo $response->num_pages; // '2' echo $response->transactions[0]->id; // '5018f833f8182b129c00002f'
交易将始终有一个id
属性,这是通过Coinbase api识别它们的主要方式。一旦它们被广播到网络(通常在几秒内),它们还将有一个hsh
(比特币哈希)属性。
检查比特币价格
通过传递您想要购买或出售的比特币的数量quantity
来检查购买或出售价格。此价格包括Coinbase的1%费用和0.15美元的银行转账费。
echo $coinbase->getBuyPrice('1'); // '125.31' echo $coinbase->getSellPrice('1'); // '122.41'
购买或出售比特币
购买和出售比特币需要您首先通过web界面链接并验证银行账户。
然后您可以调用 buy
或 sell
并传递您想要购买的比特币数量 $quantity
。
在购买时,我们将从您的银行账户扣除款项,比特币将在四个工作日后到达您的Coinbase账户(这显示为下面的 payout_date
)。这是银行转账完成和验证所需的时间,尽管我们正在努力缩短这个时间窗口。在某些情况下,我们可能无法保证价格,购买请求可能会失败。在这种情况下,将第二个参数($agreeBtcAmountVaries
)设置为 true,以便在您的资金到达时以未来市场价格购买比特币。
在出售时,我们以类似的方式将款项存入您的银行账户,并在两个工作日内到账。
$response = $coinbase->buy('1.0'); echo $response->transfer->code; // '6H7GYLXZ' echo $response->transfer->btc->amount; // '1.00000000' echo $response->transfer->total->amount; // '$17.95' echo $response->transfer->payout_date; // '2013-02-01T18:00:00-08:00' (ISO 8601 format - can be parsed with the strtotime() function)
$response = $coinbase->sell('1.0'); echo $response->transfer->code; // 'RD2OC8AL' echo $response->transfer->btc->amount; // '1.00000000' echo $response->transfer->total->amount; // '$17.95' echo $response->transfer->payout_date; // '2013-02-01T18:00:00-08:00' (ISO 8601 format - can be parsed with the strtotime() function)
创建支付按钮
这将创建一个支付按钮(以及模态窗口)的代码,您可以使用它来在您的网站上接受比特币。您可以在此处了解更多关于支付按钮的信息,并尝试一个演示。
方法签名是 public function createButton($name, $price, $currency, $custom=null, $options=array())
。custom
参数将通过 回调 传递到您的网站。有效的 options
列表 在此处描述。
$response = $coinbase->createButton("Your Order #1234", "42.95", "EUR", "my custom tracking code for this order", array( "description" => "1 widget at €42.95" )); echo $response->button->code; // '93865b9cae83706ae59220c013bc0afd' echo $response->embedHtml; // '<div class=\"coinbase-button\" data-code=\"93865b9cae83706ae59220c013bc0afd\"></div><script src=\"https://coinbase.com/assets/button.js\" type=\"text/javascript\"></script>'
汇率和货币工具
您可以使用 getCurrencies()
方法获取所有支持货币及其ISO代码的列表。
$currencies = $coinbase->getCurrencies(); echo $currencies[0]->name; // 'Afghan Afghani (AFN)'
getExchangeRate()
将返回汇率列表。传递两个参数以获取单个汇率。
$rates = $coinbase->getExchangeRate(); echo $rates->btc_to_cad; // '117.13892' echo $coinbase->getExchangeRate('btc', 'cad'); // '117.13892'
创建新用户
$response = $coinbase->createUser("newuser@example.com", "some password"); echo $response->user->email; // 'newuser@example.com' echo $response->user->receive_address; // 'mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x'
如果需要立即向新用户发送付款,也会返回接收地址。
获取自动完成联系人
这将返回用户先前发送或接收的联系人列表。对于自动完成很有用。默认情况下,每次返回 30 个联系人;使用 $page
和 $limit
参数来调整分页工作。
$response = $coinbase->getContacts("exa"); echo implode(', ', $response->contacts); // 'user1@example.com, user2@example.com'
添加新方法
您可以在此处查看方法调用列表及其实现方式。它们是围绕Coinbase JSON API 的包装。
如果API参考 中有任何没有在库中有明确函数名称的方法,您也可以使用 get
、post
、put
或 delete
与 $path
和可选的 $params
数组进行快速实现。将返回原始 JSON 对象。例如
var_dump($coinbase->get('/account/balance')); // object(stdClass)#4 (2) { // ["amount"]=> // string(10) "0.56902981" // ["currency"]=> // string(3) "BTC" // }
或者您也可以自由添加新的包装方法并提交一个拉取请求。
OAuth认证
要使用OAuth进行认证,首先在https://coinbase.com/oauth/applications 创建OAuth应用程序。当用户希望连接他们的Coinbase账户时,将他们重定向到使用 Coinbase_OAuth::createAuthorizeUrl
创建的URL。
$coinbaseOauth = new Coinbase_OAuth($_CLIENT_ID, $_CLIENT_SECRET, $_REDIRECT_URL); header("Location: " . $coinbaseOauth->createAuthorizeUrl("all"));
用户授权您的应用程序后,他们将被重定向回上面指定的重定向URL。将包含一个 code
参数 - 将其传递给 getTokens
以获取一组令牌
$tokens = $coinbaseOauth->getTokens($_GET['code']);
安全存储这些令牌,并使用它们在未来进行Coinbase API请求。例如
$coinbase = new Coinbase($coinbaseOauth, $tokens); $coinbase->getBalance();
完整的示例实现可以在 example
目录中找到。
安全注意事项
如果有人获取了您的API密钥,他们将完全控制您的Coinbase账户。这包括将所有比特币发送到其他地方的能力。
因此,默认情况下,Coinbase账户上禁用了API访问。如果您决定启用API密钥访问,应采取措施在您的应用程序中安全地存储API密钥。具体如何操作取决于应用程序,但这是您应该研究的内容,如果您之前从未这样做过的话。[了解更多](http://programmers.stackexchange.com/questions/65601/is-it-smart-to-store-application-keys-ids-etc-directly-inside-an-application)。
测试
如果您想贡献代码或修改这个库,可以通过在浏览器或命令行中使用php
执行/path/to/coinbase-php/test/Coinbase.php
来运行测试套件。