phpcoinbase-apiccxtcoinbase-php

How to fetch all the coinbase pro transactions correctly in php


I have a question of coinbase pro,

How can I get all the orders/transactions from coinbase pro using php without any limit , or with any limit parameter.

I am using the following library to fetch all the coinbase pro orders , but it only gives me 100 orders from the below method.

fetch_orders

Here is the method,

$CoinbaseProtrans = $CoinbasePro->fetch_orders('ETH/USD',"",150,array('limit'=>100,'after'=>1));
echo "<pre>";print_r($CoinbaseProtrans);echo "</pre>";

Here is the error I am getting,

Fatal error: Uncaught ccxt\ExchangeError: coinbasepro after cursor value is not valid in coinbasepro.php:813 

Here is the link of the library:

https://github.com/ccxt/ccxt/blob/master/php/coinbasepro.php

Solution

  • This question was answered here: https://github.com/ccxt/ccxt/issues/6105#issuecomment-552405563

    <?php
    
    include_once ('ccxt.php');
    
    date_default_timezone_set ('UTC');
    
    $exchange = new \ccxt\coinbasepro(array(
        'apiKey' => 'YOUR_API_KEY',
        'secret' => 'YOUR_SECRET',
        // 'verbose' => true, // uncomment for debugging
        // https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
        'enableRateLimit' => true, // rate-limiting is required by the Manual
    ));
    
    $exchange->load_markets ();
    
    // $exchange->verbose = true; // uncomment for debugging
    
    $all_results = array();
    
    $symbol = 'ETH/USD';
    $since = null;
    $limit = 100;
    $params = array();
    
    do {
        // any of the following methods should work:
        // $results = $exchange->fetch_orders($symbol, $since, $limit, $params);
        // $results = $exchange->fetch_my_trades($symbol, $since, $limit, $params);
        $results = $exchange->fetch_trades($symbol, $since, $limit, $params);
        echo $exchange->iso8601($exchange->milliseconds());
        echo ' fetched ' . count($results) . " results\n";
        $all_results = array_merge ($all_results, $results);
        if (count($results) > 0) {
            $last = count($results) - 1;
            echo '     last result ' . $results[$last]['id'] . ' ' . $results[$last]['datetime'] . "\n";
            echo '    first result ' . $results[0]['id'] . ' ' . $results[0]['datetime'] . "\n";
        } else {
            break;
        }
        @$params['after'] = $exchange->last_response_headers['cb-after'][0];
    
    // uncomment one of the following:
    // } while (true); // fetch all results forever
    } while (count($all_results) < 1000); // fetch up to 1000 results
    
    echo "fetched " . count($all_results) . " results in total\n";
    
    ?>