user6043723
user6043723

Reputation: 177

PHP get value from class without modifying existing class

I am using the following Coinbase PHP api:

The class file includes the following line:

 * An array of API endpoints
 */
 public $endpoints = array(
 'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),
 );

 public function getOrderBook($product = 'BTC-EUR') {
 //$this->validate('product', $product);
 return $this->request('book', array('id' => $product));
 }

In my file I call it using:

$exchange = new CoinbaseExchange();//Connect to Coinbase API
$getOrderbook = $exchange->getOrderBook();
print_r($getOrderbook);

Nothing is returned.

Though if I modify the class from:

'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),

To:

'book' => array('method' => 'GET', 'uri' => '/products/%s/book?level=2'),

I get the desired out put in my file.

How can I leave the class as 'book' => array('method' => 'GET', 'uri' => '/products/%s/book'), as it is calling it through $getOrderbook = $exchange->getOrderBook();. Where do I include 'level=2` in the latter line please?

Upvotes: 0

Views: 42

Answers (2)

Justinas
Justinas

Reputation: 43479

You can make temporary changes to endpoint:

$oldEndpoint= $exchange->endpoints['book']; // save previous value
$exchange->endpoints['book']['uri'] .= '?level=2'; // make needed changes
$exchange->getOrderBook();
$exchange->endpoints['book'] = $oldEndpoint; // reset to old value

Upvotes: 0

kamal pal
kamal pal

Reputation: 4207

As the property $endpoints is public, you can access it from outside of class (see below):

$exchange = new CoinbaseExchange();//Connect to Coinbase API
$exchange->endpoints = array('book' => array('method' => 'GET', 'uri' => '/products/%s/book?level=2'))
$getOrderbook = $exchange->getOrderBook();
print_r($getOrderbook);

Upvotes: 2

Related Questions