coinbase api 调用导致“403 Forbidden”响应

coinbase api call resulted in a `403 Forbidden` response

我正在尝试集成 Coinbase 钱包 PHP 库。

这是我获取当前 BTC 买入价的代码

<?php

require_once 'vendor/autoload.php';

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Resource\Address;
use Coinbase\Wallet\Resource\Account;

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

try {
    $buyPrice = $client->getBuyPrice('BTC-USD');
} catch (Exception $exc) {
    echo '<pre>';
    echo $exc->getTraceAsString();
    echo '<hr/>';
    echo $exc->getMessage();
    echo '</pre>';

}
?>

以上代码运行良好,但有时会抛出以下错误

#0 C:\xampp\htdocs\coinbase\vendor\coinbase\coinbase\src\HttpClient.php(137): Coinbase\Wallet\Exception\HttpException::wrap(Object(GuzzleHttp\Exception\ClientException))
#1 C:\xampp\htdocs\coinbase\vendor\coinbase\coinbase\src\HttpClient.php(121): Coinbase\Wallet\HttpClient->send(Object(GuzzleHttp\Psr7\Request), Array)
#2 C:\xampp\htdocs\coinbase\vendor\coinbase\coinbase\src\HttpClient.php(74): Coinbase\Wallet\HttpClient->request('GET', '/v2/prices/BTC-...', Array)
#3 C:\xampp\htdocs\coinbase\vendor\coinbase\coinbase\src\Client.php(820): Coinbase\Wallet\HttpClient->get('/v2/prices/BTC-...', Array)
#4 C:\xampp\htdocs\coinbase\vendor\coinbase\coinbase\src\Client.php(118): Coinbase\Wallet\Client->getAndMapMoney('/v2/prices/BTC-...', Array)
#5 C:\xampp\htdocs\coinbase\index.php(16): Coinbase\Wallet\Client->getBuyPrice('BTC-USD')
#6 {main}
Client error: `GET https://api.coinbase.com/v2/prices/BTC-USD/buy` resulted in a `403 Forbidden` response:

求推荐。

谢谢

我在连接这个 API 时遇到了类似的问题,但没有使用该库。

我认为这是来自 Cloudflare 的某种保护。我设法绕过了它,添加了一些额外的 headers 来伪造 "real" 浏览器,尤其是 "User-Agent":

        $url = 'https://api.coinbase.com/v2/prices/BTC-EUR/spot';
        $response = wp_remote_get($url, array(
                'timeout' => 120,
                'headers' => array(
                    'CB-VERSION' => '2017-05-19',
                    'Accept' => '*/*',
                    'Accept-Language' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Cache-Control' => 'no-cache',
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 OPR/45.0.2552.882'
                )
            )
        );

(wp_remote_get 来自 WordPress)