首頁 > 後端開發 > php教程 > PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟)

PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟)

不言
發布: 2023-04-04 10:30:02
轉載
5599 人瀏覽過

這篇文章帶給大家的內容是關於PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

這是Coinbase Wallet API v2的官方客戶端函式庫。我們提供直觀,穩定的介面,將Coinbase Wallet整合到的PHP項目中。

重要:由於此程式庫是針對較新的API v2的,因此需要v2權限(即wallet:accounts:read)。如果你仍在使用v1,請使用此函式庫的舊版本。

安裝

使用Composer安裝函式庫。如果你不熟悉Composer或依賴管理器,請閱讀Composer文件。

1

2

3

"require": {

    "coinbase/coinbase""~2.0"

}

登入後複製

認證

API金鑰

使用API​​金鑰和金鑰存取你自己的Coinbase帳號。

1

2

3

4

5

use Coinbase\Wallet\Client;

use Coinbase\Wallet\Configuration;

 

$configuration = Configuration::apiKey($apiKey$apiSecret);

$client = Client::create($configuration);

登入後複製

OAuth2

使用OAuth2驗證存取你自己以外的使用者帳號。此函式庫不處理握手過程,並假定你在初始化時具有存取token。你可以使用OAuth2客戶端(例如league/oauth2-client)處理握手過程。

1

2

3

4

5

6

7

8

9

10

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令牌(在此處閱讀更多)。如果需要,則拋出特定異常。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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',

    ]);

}

登入後複製

分頁

幾個端點是分頁的。預設情況下,庫只會取得給定請求的第一頁資料。你可以輕鬆加載不僅僅是第一頁結果。

1

2

3

4

$transactions $client->getAccountTransactions($account);

while ($transactions->hasNextPage()) {

    $client->loadNextTransactions($transactions);

}

登入後複製

你也可以使用fetch_all參數讓函式庫發出載入完整集合的所有必要請求。

1

2

3

4

5

use Coinbase\Wallet\Enum\Param;

 

$transactions $client->getAccountTransactions($account, [

    Param::FETCH_ALL => true,

]);

登入後複製

 警告

注意警告是明智的。如果配置了一個標準PSR-3記錄器,庫將記錄所有警告。

1

2

3

4

5

6

use Coinbase\Wallet\Client;

use Coinbase\Wallet\Configuration;

 

$configuration = Configuration::apiKey($apiKey$apiSecret);

$configuration->setLogger($logger);

$client = Client::create($configuration);

登入後複製

資源參考

在某些情況下,API會傳回資源參考來取代擴充的資源物件。可以透過刷新來擴展這些引用。

1

2

3

4

5

$deposit $this->client->getAccountDeposit($account$depositId);

$transaction $deposit->getTransaction();

if (!$transaction->isExpanded()) {

    $this->client->refreshTransaction($transaction);

}

登入後複製

你也可以使用expand參數請求API在初始請求中傳回擴充資源。

1

2

3

4

5

use Coinbase\Wallet\Enum\Param;

 

$deposit $this->client->getAccountDeposit($account$depositId, [

    Param::EXPAND = ['transaction'],

]);

登入後複製

建立新資源時可以使用資源引用,從而避免從API請求資源的開銷。

1

2

3

4

5

6

7

8

9

10

11

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()方法,你可以使用該方法存取未對應到物件屬性的任何欄位。

1

$data $deposit->getRawData();

登入後複製

來自最後一個HTTP回應的原始資料也可在客戶端物件上使用。

1

$data $client->decodeLastResponse();

登入後複製

活動記錄方法

此程式庫包含對資源物件上的活動記錄方法的支援。你必須在引導應用程式時啟用此功能。

1

$client->enableActiveRecord();

登入後複製

啟用後,你可以在資源物件上呼叫活動記錄方法。

1

2

3

4

5

use Coinbase\Wallet\Enum\Param;

 

$transactions $account->getTransactions([

    Param::FETCH_ALL => true,

]);

登入後複製

用法

這不是為了提供API的完整文件。有關更多詳細信息,請參閱官方文件。

市場數據

列出支援的本地貨幣

1

$currencies $client->getCurrencies();

登入後複製

列出匯率

1

$rates $client->getExchangeRates();

登入後複製

買入價

1

$buyPrice $client->getBuyPrice('BTC-USD');

登入後複製

賣出價

1

$sellPrice $client->getSellPrice('BTC-USD');

登入後複製

現貨價格

1

$spotPrice $client->getSpotPrice('BTC-USD');

登入後複製

目前伺服器時間

1

$time $client->getTime();

登入後複製

使用者

取得授權資訊

1

$auth $client->getCurrentAuthorization();

登入後複製
登入後複製

尋找使用者資訊

1

$auth $client->getCurrentAuthorization();

登入後複製
登入後複製

取得目前使用者

1

$user $client->getCurrentUser();

登入後複製

更新目前使用者

1

2

$user->setName('New Name');

$client->updateCurrentUser($user);

登入後複製

帳號

#列出所有帳號

1

$accounts $client->getAccounts();

登入後複製

列出帳戶詳細資料

1

$account $client->getAccount($accountId);

登入後複製

列出主要帳戶詳細資訊

1

$account $client->getPrimaryAccount();

登入後複製

將帳戶設為主要帳戶

1

$client->setPrimaryAccount($account);

登入後複製

建立一個新的比特幣帳戶

1

2

3

4

5

6

use Coinbase\Wallet\Resource\Account;

 

$account new Account([

    'name' => 'New Account'

]);

$client->createAccount($account);

登入後複製

更新帳戶

1

2

$account->setName('New Account Name');

$client->updateAccount($account):

登入後複製

刪除帳戶

1

$client->deleteAccount($account);

登入後複製

位址

列出帳戶的接收位址

1

$addresses $client->getAccountAddresses($account);

登入後複製

取得接收位址資訊

1

$address $client->getAccountAddress($account$addressId);

登入後複製

列出位址的交易

1

$transactions $client->getAddressTransactions($address);

登入後複製

建立一個新的接收位址

1

2

3

4

5

6

use Coinbase\Wallet\Resource\Address;

 

$address new Address([

    'name' => 'New Address'

]);

$client->createAccountAddress($account$address);

登入後複製

交易

列出交易清單

1

$transactions $client->getAccountTransactions($account);

登入後複製

取得交易資訊

1

$transaction $client->getAccountTransaction($account$transactionId);

登入後複製

發送資金

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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(); 

}

登入後複製

將資金轉入新帳戶

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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);

登入後複製

申請資金

1

2

3

4

5

6

7

8

9

10

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);

登入後複製

重新發送請求

1

$account->resendTransaction($transaction);

登入後複製

取消請求

1

$account->cancelTransaction($transaction);

登入後複製

完成請求

1

$account->completeTransaction($transaction);

登入後複製

買入

列出購買清單

1

$buys $client->getAccountBuys($account);

登入後複製

取得購買資訊

1

$buy $client->getAccountBuy($account$buyId);

登入後複製

買入比特幣

1

2

3

4

5

6

7

use Coinbase\Wallet\Resource\Buy;

 

$buy new Buy([

    'bitcoinAmount' => 1

]);

 

$client->createAccountBuy($account$buy);

登入後複製

購買確​​認

如果在建立購買時傳遞commit=false,則只需執行此操作。

1

2

3

4

use Coinbase\Wallet\Enum\Param;

 

$client->createAccountBuy($account$buy, [Param::COMMIT => false]);

$client->commitBuy($buy);

登入後複製

賣出

出售清單

1

$sells $client->getAccountSells($account);

登入後複製

取得銷售資訊

1

$sell $client->getAccountSell($account$sellId);

登入後複製

賣比特幣

1

2

3

4

5

6

7

use Coinbase\Wallet\Resource\Sell;

 

$sell new Sell([

    'bitcoinAmount' => 1

]);

 

$client->createAccountSell($account$sell);

登入後複製

出售確認

#如果在建立sell時傳遞commit=false,則只需執行此操作。

1

2

3

4

use Coinbase\Wallet\Enum\Param;

 

$client->createAccountSell($account$sell, [Param::COMMIT => false]);

$client->commitSell($sell);

登入後複製

存款

列出存款清單

1

$deposits $client->getAccountDeposits($account);

登入後複製

取得存款資訊

1

$deposit $client->getAccountDeposit($account$depositId);

登入後複製

存款

1

2

3

4

5

6

7

8

9

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);

登入後複製

提交押金

如果在建立存款時傳遞

commit=false

,則只需執行此操作。

1

2

3

4

use Coinbase\Wallet\Enum\Param;

 

$client->createAccountDeposit($account$deposit, [Param::COMMIT => false]);

$client->commitDeposit($deposit);

登入後複製

提款

列出提款單

1

$withdrawals $client->getAccountWithdrawals($account);

登入後複製

取消

1

$withdrawal $client->getAccountWithdrawal($account$withdrawalId);

登入後複製

提款

1

2

3

4

5

6

7

8

9

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###,則只需執行此操作。 ###

1

2

3

4

use Coinbase\Wallet\Enum\Param;

 

$client->createAccountWithdrawal($account$withdrawal, [Param::COMMIT => false]);

$client->commitWithdrawal($withdrawal);

登入後複製
###付款方式######列出付款方式###

1

$paymentMethods $client->getPaymentMethods();

登入後複製
###取得付款方式###

1

$paymentMethod $client->getPaymentMethod($paymentMethodId);

登入後複製
###商家######取得商家###

1

$merchant $client->getMerchant($merchantId);

登入後複製
# ##訂單######列出訂單###

1

$orders $client->getOrders();

登入後複製
###獲得訂單###

1

$order $client->getOrder($orderId);

登入後複製
###建立訂單###

1

2

3

4

5

6

7

8

9

use Coinbase\Wallet\Resource\Order;

use Coinbase\Wallet\Value\Money;

 

$order new Order([

    'name' => 'Order #1234',

    'amount' => Money::btc(1)

]);

 

$client->createOrder($order);

登入後複製
###退款訂單###

1

2

3

use Coinbase\Wallet\Enum\CurrencyCode;

 

$client->refundOrder($order, CurrencyCode::BTC);

登入後複製
###結帳## ####列出結帳單###

1

$checkouts $client->getCheckouts();

登入後複製
###建立結帳單###

1

2

3

4

5

6

7

8

9

10

11

12

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";

登入後複製
###結帳###

1

$checkout $client->getCheckout($checkoutId);

登入後複製
###取得結帳的訂單###

1

$orders $client->getCheckoutOrders($checkout);

登入後複製
###建立結帳訂單###

1

$order $client->createNewCheckoutOrder($checkout);

登入後複製

通知webhook和验证

1

2

3

$raw_body file_get_contents('php://input');

$signature $_SERVER['HTTP_CB_SIGNATURE'];

$authenticity $client->verifyCallback($raw_body$signature); // boolean

登入後複製

贡献和测试

测试套件使用PHPUnit构建。通过运行phpunit命令运行单元测试套件。

1

phpunit

登入後複製

还有一组集成测试,它们向API发出实际请求并检查生成的对象。要运行这些测试,必须将phpunit.xml.dist复制到phpunit.xml,为CB_API_KEYCB_API_SECRET变量提供值,并在运行测试套件时指定integration组。

1

phpunit --group integration

登入後複製

以上是PHP如何使用比特幣Coinbase錢包庫開發應用程式(詳細步驟)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板