duanjianl183188 2017-06-14 07:23
浏览 152
已采纳

coinbase api调用导致了'403 Forbidden`响应

I'm trying to integrate Coinbase Wallet PHP Library.

here is my code to get current BTC buy price

<?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>';

}
?>

above code work perfectly, but some time it throw following error

#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:

Please suggest.

Thanks

  • 写回答

1条回答 默认 最新

  • doulangpeng3933 2017-06-15 19:17
    关注

    I had a similiar issue connecting this API, but not using the libary.

    I think it is some kind of protection from Cloudflare. I managed to circumvent it adding some additional headers to fake a "real" browser, especially the "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 is from WordPress)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分