這篇文章帶給大家的內容是關於PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
這是Coinbase Wallet API v2的官方客戶端函式庫。我們提供直觀,穩定的介面,將Coinbase Wallet整合到的PHP項目中。
重要:由於此程式庫是針對較新的API v2的,因此需要v2權限(即wallet:accounts:read
)。如果你仍在使用v1,請使用此函式庫的舊版本。
使用Composer安裝函式庫。如果你不熟悉Composer或依賴管理器,請閱讀Composer文件。
"require": { "coinbase/coinbase": "~2.0" }
使用API金鑰和金鑰存取你自己的Coinbase帳號。
use Coinbase\Wallet\Client; use Coinbase\Wallet\Configuration; $configuration = Configuration::apiKey($apiKey, $apiSecret); $client = Client::create($configuration);
使用OAuth2驗證存取你自己以外的使用者帳號。此函式庫不處理握手過程,並假定你在初始化時具有存取token。你可以使用OAuth2客戶端(例如league/oauth2-client)處理握手過程。
use Coinbase\Wallet\Client; use Coinbase\Wallet\Configuration; // with a refresh token $configuration = Configuration::oauth($accessToken, $refreshToken); // without a refresh token $configuration = Configuration::oauth($accessToken); $client = Client::create($configuration);
發送資金端點在某些情況下需要2FA令牌(在此處閱讀更多)。如果需要,則拋出特定異常。
use Coinbase\Wallet\Enum\Param; use Coinbase\Wallet\Exception\TwoFactorRequiredException; use Coinbase\Wallet\Resource\Transaction; $transaction = Transaction::send([ 'toEmail' => 'test@test.com', 'bitcoinAmount' => 1 ]); $account = $client->getPrimaryAccount(); try { $client->createAccountTransaction($account, $transaction); } catch (TwoFactorRequiredException $e) { // show 2FA dialog to user and collect 2FA token // retry call with token $client->createAccountTransaction($account, $transaction, [ Param::TWO_FACTOR_TOKEN => '123456', ]); }
幾個端點是分頁的。預設情況下,庫只會取得給定請求的第一頁資料。你可以輕鬆加載不僅僅是第一頁結果。
$transactions = $client->getAccountTransactions($account); while ($transactions->hasNextPage()) { $client->loadNextTransactions($transactions); }
你也可以使用fetch_all
參數讓函式庫發出載入完整集合的所有必要請求。
use Coinbase\Wallet\Enum\Param; $transactions = $client->getAccountTransactions($account, [ Param::FETCH_ALL => true, ]);
注意警告是明智的。如果配置了一個標準PSR-3記錄器,庫將記錄所有警告。
use Coinbase\Wallet\Client; use Coinbase\Wallet\Configuration; $configuration = Configuration::apiKey($apiKey, $apiSecret); $configuration->setLogger($logger); $client = Client::create($configuration);
在某些情況下,API會傳回資源參考來取代擴充的資源物件。可以透過刷新來擴展這些引用。
$deposit = $this->client->getAccountDeposit($account, $depositId); $transaction = $deposit->getTransaction(); if (!$transaction->isExpanded()) { $this->client->refreshTransaction($transaction); }
你也可以使用expand
參數請求API在初始請求中傳回擴充資源。
use Coinbase\Wallet\Enum\Param; $deposit = $this->client->getAccountDeposit($account, $depositId, [ Param::EXPAND = ['transaction'], ]);
建立新資源時可以使用資源引用,從而避免從API請求資源的開銷。
use Coinbase\Wallet\Resource\Deposit; use Coinbase\Wallet\Resource\PaymentMethod; $deposit = new Deposit([ 'paymentMethod' => PaymentMethod::reference($paymentMethodId) ]); // or use the convenience method $deposit = new Deposit([ 'paymentMethodId' => $paymentMethodId ]);
有多種方法可以存取原始回應資料。首先,每個資源物件都有一個getRawData()
方法,你可以使用該方法存取未對應到物件屬性的任何欄位。
$data = $deposit->getRawData();
來自最後一個HTTP回應的原始資料也可在客戶端物件上使用。
$data = $client->decodeLastResponse();
此程式庫包含對資源物件上的活動記錄方法的支援。你必須在引導應用程式時啟用此功能。
$client->enableActiveRecord();
啟用後,你可以在資源物件上呼叫活動記錄方法。
use Coinbase\Wallet\Enum\Param; $transactions = $account->getTransactions([ Param::FETCH_ALL => true, ]);
這不是為了提供API的完整文件。有關更多詳細信息,請參閱官方文件。
列出支援的本地貨幣
$currencies = $client->getCurrencies();
列出匯率
$rates = $client->getExchangeRates();
買入價
$buyPrice = $client->getBuyPrice('BTC-USD');
賣出價
$sellPrice = $client->getSellPrice('BTC-USD');
現貨價格
$spotPrice = $client->getSpotPrice('BTC-USD');
目前伺服器時間
$time = $client->getTime();
取得授權資訊
$auth = $client->getCurrentAuthorization();
尋找使用者資訊
$auth = $client->getCurrentAuthorization();
取得目前使用者
$user = $client->getCurrentUser();
更新目前使用者
$user->setName('New Name'); $client->updateCurrentUser($user);
#列出所有帳號
$accounts = $client->getAccounts();
列出帳戶詳細資料
$account = $client->getAccount($accountId);
列出主要帳戶詳細資訊
$account = $client->getPrimaryAccount();
將帳戶設為主要帳戶
$client->setPrimaryAccount($account);
建立一個新的比特幣帳戶
use Coinbase\Wallet\Resource\Account; $account = new Account([ 'name' => 'New Account' ]); $client->createAccount($account);
更新帳戶
$account->setName('New Account Name'); $client->updateAccount($account):
刪除帳戶
$client->deleteAccount($account);
列出帳戶的接收位址
$addresses = $client->getAccountAddresses($account);
取得接收位址資訊
$address = $client->getAccountAddress($account, $addressId);
列出位址的交易
$transactions = $client->getAddressTransactions($address);
建立一個新的接收位址
use Coinbase\Wallet\Resource\Address; $address = new Address([ 'name' => 'New Address' ]); $client->createAccountAddress($account, $address);
列出交易清單
$transactions = $client->getAccountTransactions($account);
取得交易資訊
$transaction = $client->getAccountTransaction($account, $transactionId);
發送資金
use Coinbase\Wallet\Enum\CurrencyCode; use Coinbase\Wallet\Resource\Transaction; use Coinbase\Wallet\Value\Money; $transaction = Transaction::send([ 'toBitcoinAddress' => 'ADDRESS', 'amount' => new Money(5, CurrencyCode::USD), 'description' => 'Your first bitcoin!', 'fee' => '0.0001' // only required for transactions under BTC0.0001 ]); try { $client->createAccountTransaction($account, $transaction); } catch(Exception $e) { echo $e->getMessage(); }
將資金轉入新帳戶
use Coinbase\Wallet\Resource\Transaction; use Coinbase\Wallet\Resource\Account; $fromAccount = Account::reference($accountId); $toAccount = new Account([ 'name' => 'New Account' ]); $client->createAccount($toAccount); $transaction = Transaction::transfer([ 'to' => $toAccount, 'bitcoinAmount' => 1, 'description' => 'Your first bitcoin!' ]); $client->createAccountTransaction($fromAccount, $transaction);
申請資金
use Coinbase\Wallet\Enum\CurrencyCode; use Coinbase\Wallet\Resource\Transaction; use Coinbase\Wallet\Value\Money; $transaction = Transaction::request([ 'amount' => new Money(8, CurrencyCode::USD), 'description' => 'Burrito' ]); $client->createAccountTransaction($transaction);
重新發送請求
$account->resendTransaction($transaction);
取消請求
$account->cancelTransaction($transaction);
完成請求
$account->completeTransaction($transaction);
列出購買清單
$buys = $client->getAccountBuys($account);
取得購買資訊
$buy = $client->getAccountBuy($account, $buyId);
買入比特幣
use Coinbase\Wallet\Resource\Buy; $buy = new Buy([ 'bitcoinAmount' => 1 ]); $client->createAccountBuy($account, $buy);
購買確認
如果在建立購買時傳遞commit=false
,則只需執行此操作。
use Coinbase\Wallet\Enum\Param; $client->createAccountBuy($account, $buy, [Param::COMMIT => false]); $client->commitBuy($buy);
出售清單
$sells = $client->getAccountSells($account);
取得銷售資訊
$sell = $client->getAccountSell($account, $sellId);
賣比特幣
use Coinbase\Wallet\Resource\Sell; $sell = new Sell([ 'bitcoinAmount' => 1 ]); $client->createAccountSell($account, $sell);
出售確認
#如果在建立sell時傳遞commit=false
,則只需執行此操作。
use Coinbase\Wallet\Enum\Param; $client->createAccountSell($account, $sell, [Param::COMMIT => false]); $client->commitSell($sell);
列出存款清單
$deposits = $client->getAccountDeposits($account);
取得存款資訊
$deposit = $client->getAccountDeposit($account, $depositId);
存款
use Coinbase\Wallet\Enum\CurrencyCode; use Coinbase\Wallet\Resource\Deposit; use Coinbase\Wallet\Value\Money; $deposit = new Deposit([ 'amount' => new Money(10, CurrencyCode::USD) ]); $client->createAccountDeposit($account, $deposit);
提交押金
,則只需執行此操作。
use Coinbase\Wallet\Enum\Param; $client->createAccountDeposit($account, $deposit, [Param::COMMIT => false]); $client->commitDeposit($deposit);
提款
列出提款單
$withdrawals = $client->getAccountWithdrawals($account);
取消
$withdrawal = $client->getAccountWithdrawal($account, $withdrawalId);
提款
use Coinbase\Wallet\Enum\CurrencyCode; use Coinbase\Wallet\Resource\Withdrawal; use Coinbase\Wallet\Value\Money; $withdrawal = new Withdrawal([ 'amount' => new Money(10, CurrencyCode::USD) ]); $client->createAccountWithdrawal($account, $withdrawal);###提交退出##### #如果在呼叫提款方法時傳遞###commit=true###,則只需執行此操作。 ###
use Coinbase\Wallet\Enum\Param; $client->createAccountWithdrawal($account, $withdrawal, [Param::COMMIT => false]); $client->commitWithdrawal($withdrawal);###付款方式######列出付款方式###
$paymentMethods = $client->getPaymentMethods();###取得付款方式###
$paymentMethod = $client->getPaymentMethod($paymentMethodId);###商家######取得商家###
$merchant = $client->getMerchant($merchantId);# ##訂單######列出訂單###
$orders = $client->getOrders();###獲得訂單###
$order = $client->getOrder($orderId);###建立訂單###
use Coinbase\Wallet\Resource\Order; use Coinbase\Wallet\Value\Money; $order = new Order([ 'name' => 'Order #1234', 'amount' => Money::btc(1) ]); $client->createOrder($order);###退款訂單###
use Coinbase\Wallet\Enum\CurrencyCode; $client->refundOrder($order, CurrencyCode::BTC);###結帳## ####列出結帳單###
$checkouts = $client->getCheckouts();###建立結帳單###
use Coinbase\Wallet\Resource\Checkout; $params = array( 'name' => 'My Order', 'amount' => new Money(100, 'USD'), 'metadata' => array( 'order_id' => $custom_order_id ) ); $checkout = new Checkout($params); $client->createCheckout($checkout); $code = $checkout->getEmbedCode(); $redirect_url = "https://www.coinbase.com/checkouts/$code";###結帳###
$checkout = $client->getCheckout($checkoutId);###取得結帳的訂單###
$orders = $client->getCheckoutOrders($checkout);###建立結帳訂單###
$order = $client->createNewCheckoutOrder($checkout);
$raw_body = file_get_contents('php://input'); $signature = $_SERVER['HTTP_CB_SIGNATURE']; $authenticity = $client->verifyCallback($raw_body, $signature); // boolean
测试套件使用PHPUnit构建。通过运行phpunit
命令运行单元测试套件。
phpunit
还有一组集成测试,它们向API发出实际请求并检查生成的对象。要运行这些测试,必须将phpunit.xml.dist
复制到phpunit.xml
,为CB_API_KEY
和CB_API_SECRET
变量提供值,并在运行测试套件时指定integration
组。
phpunit --group integration
以上是PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟)的詳細內容。更多資訊請關注PHP中文網其他相關文章!