无法使用Coinbase PHP API购买ETH或其他代币

时间:2019-04-09 02:26:57

标签: php api coinbase-api

我当前正在使用Coinbase php API。我尝试用以下代码购买ETH:

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Resource;
use Coinbase\Wallet\Resource\Sell;
use Coinbase\Wallet\Resource\Buy;
use Coinbase\Wallet\Enum\Param;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Account;
use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Exception;
use GuzzleHttp\Exception\ClientException;
use Coinbase\Wallet\Resource\Transaction;


$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);  

$accounts = $client->getAccounts();
$account = $client->getPrimaryAccount(); // or some other account

$buy = new Buy([
'amount' => new Money(0.05, CurrencyCode::ETH), 
'currency' => CurrencyCode::ETH
]);

$client->createAccountBuy($accounts, $buy, [Param::COMMIT => false]);
//$selldetail = $client->commitSell($sell);


$PaymentDetail = $client->decodeLastResponse();
//$PaymentArray =  $PaymentDetail['data'];


echo '<pre>';
print_r($PaymentDetail);
echo '</pre>';


代码抛出此错误:

  

未捕获的消息TypeError:参数   传递给Coinbase \ Wallet \ Client :: createAccountBuy()的1必须是   Coinbase \ Wallet \ Resource \ Account的实例,   给定的Coinbase \ Wallet \ Resource \ ResourceCollection,称为   第154行的C:\ xampp \ htdocs \ dev \ coinbase \ cb_coin.php并在   C:\ xampp \ htdocs \ dev \ coinbase \ src \ Client.php:421堆栈跟踪:#0   C:\ xampp \ htdocs \ dev \ coinbase \ cb_coin.php(154):   Coinbase \ Wallet \ Client-> createAccountBuy(Object(Coinbase \ Wallet \ Resource \ ResourceCollection),Object(Coinbase \ Wallet \ Resource \ Buy),Array)#1 {main}投掷

2 个答案:

答案 0 :(得分:0)

$accounts = $client->getAccounts();返回一个列表,但是$client->createAccountBuy仅期望第一个参数使用一个帐户。您可以尝试$accounts[0]来获得第一个,或者print_r($accounts)并查看要使用哪个。

答案 1 :(得分:0)

使用$ accounts将钱包ID传递给购买功能,现在可以使用