Einzelwerte aus Klasse/Funktion ausgeben


loddarmattheus

Erfahrenes Mitglied
Guten Morgen,
eine Frage an die Profis, da ich moch zu blö* anstelle:

Folgender Code gibts mir ein Array mit 39 Cryptobörsen aus, welche aber allerhand unnütze Informationen wie exchange_fee, exchange_code usw. liefert.
Was müsste ich als Abfrage stehen haben, dass es mir im Ergebnis nur die Exchange-Namen auflistet:

Bitstamp
Bitfinex
Kraken
Poloniex
....
.... usw.

Besten Dank für einen Tipp.

PHP:
<?php

class coinigy_api_example_v1  {
   
    //private class vars set in constructor
    //see API docs for more info
    private $coinigy_api_key;  
    private $coinigy_api_secret;  
    private $endpoint;
   
   
    function __construct()
    {
        //see API docs for more info
        $this->coinigy_api_key = '15ee688bf8a0166aafd59dac862f700d';
        $this->coinigy_api_secret = 'b515a26e52842153226c163b94d06155';
        $this->endpoint = 'https://api.coinigy.com/api/v1/'; //with trailing slash
    }
   
  
     public function accounts()
    {
        $post_arr = array();
        $result = $this->doWebRequest('accounts', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function activity()
    {
        $post_arr = array();
        $result = $this->doWebRequest('activity', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function balances($auth_ids)
    {
        $post_arr = array();
        $post_arr["auth_ids"] = $auth_ids;         
       
        $result = $this->doWebRequest('balances', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function pushNotifications()
    {
        $post_arr = array();
        $result = $this->doWebRequest('pushNotifications', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function user_orders()
    {
        $post_arr = array();
        $result = $this->doWebRequest('orders', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function alerts()
    {
        $post_arr = array();
        $result = $this->doWebRequest('alerts', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function exchanges()
    {
        $post_arr = array();
        $result = $this->doWebRequest('exchanges', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function markets($exchange_code)
    {
        $post_arr = array();
        $post_arr["exchange_code"] = $exchange_code; 
       
        $result = $this->doWebRequest('markets', $post_arr);       
        $this->output_result($result);          

    }
   
   
    public function history($exchange_code, $exchange_market)
    {
        $post_arr = array();       
        $post_arr["exchange_code"] = $exchange_code; 
        $post_arr["exchange_market"] = $exchange_market;
        $post_arr["type"] = "history";
       
       
        $result = $this->doWebRequest('data', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function asks($exchange_code, $exchange_market)
    {
        $post_arr = array();
        $post_arr["exchange_code"] = $exchange_code; 
        $post_arr["exchange_market"] = $exchange_market;
        $post_arr["type"] = "asks";
       
        $result = $this->doWebRequest('data', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function bids($exchange_code, $exchange_market)
    {
        $post_arr = array();
        $post_arr["exchange_code"] = $exchange_code; 
        $post_arr["exchange_market"] = $exchange_market;
        $post_arr["type"] = "bids";
       
        $result = $this->doWebRequest('data', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    //asks + bids + history
    public function data($exchange_code, $exchange_market)
    {              
        $post_arr = array();
        $post_arr["exchange_code"] = $exchange_code; 
        $post_arr["exchange_market"] = $exchange_market;
        $post_arr["type"] = "all";
       
        $result = $this->doWebRequest('data', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    //asks + bids
    public function orders($exchange_code, $exchange_market)
    {
       
        $post_arr = array();
        $post_arr["exchange_code"] = $exchange_code; 
        $post_arr["exchange_market"] = $exchange_market;
        $post_arr["type"] = "orders";
       
        $result = $this->doWebRequest('data', $post_arr);       
       
        $this->output_result($result);                 
    }
   
   
    public function newsFeed()
    {
        $post_arr = array();       
       
        $result = $this->doWebRequest('newsFeed', $post_arr);       
        $this->output_result($result);         
    }
   
   
    public function orderTypes()
    {
        $post_arr = array();
        $result = $this->doWebRequest('orderTypes', $post_arr);       
        $this->output_result($result);         
    }
   
   
    ////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////
    //////////////////////                      ////////////////////////////////////////
    /////////////            ACTION METHODS         ////////////////////////////////////
    /////////////////////                       ////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////
   
   
    public function refreshBalance($auth_id)
    {
       
        $post_arr = array();
        $post_arr["auth_id"] = $auth_id; 
       
        $result = $this->doWebRequest('refreshBalance', $post_arr);       
        $this->output_result($result);                 
    }
   
   
    public function addAlert($exchange_code, $exchange_market, $alert_price)
    {
        $post_arr = array();
        $post_arr["exch_code"] = $exchange_code; 
        $post_arr["market_name"] = $exchange_market;     
        $post_arr["alert_price"] = $alert_price;     
       
        $result = $this->doWebRequest('addAlert', $post_arr);       
        $this->output_result($result);         
    }
   
   
    public function deleteAlert($delete_alert_id)
    {
        $post_arr = array();
        $post_arr["alert_id"] = $delete_alert_id;
       
        $result = $this->doWebRequest('deleteAlert', $post_arr);       
        $this->output_result($result);         
    }
    
   
    public function addOrder($order_auth_id, $order_exch_id, $order_mkt_id, $order_type_id, $price_type_id, $limit_price, $stop_price, $order_quantity)
    {
        $post_arr = array();
        $post_arr["auth_id"] = $order_auth_id;
        $post_arr["exch_id"] = $order_exch_id;
        $post_arr["mkt_id"] = $order_mkt_id;    
        $post_arr["order_type_id"] = $order_type_id;    
        $post_arr["price_type_id"] = $price_type_id;
        $post_arr["limit_price"] =$limit_price;
        $post_arr["stop_price"] = $stop_price;          
        $post_arr["order_quantity"] = $order_quantity;          
       
        $result = $this->doWebRequest('addOrder', $post_arr);       
        $this->output_result($result);         
       
    }
   
   
    public function cancelOrder($cancel_order_id)
    {   
        $post_arr = array();
        $post_arr["internal_order_id"] = $cancel_order_id;          
       
        $result = $this->doWebRequest('cancelOrder', $post_arr);       
        $this->output_result($result);         
       
    }
   
   
    private function doWebRequest($method, $post_arr)
    {
                       
        $url = $this->endpoint.$method;
       
        $headers = array('X-API-KEY: ' . $this->coinigy_api_key,
                         'X-API-SECRET: ' . $this->coinigy_api_secret);
           
       
        // our curl handle (initialize if required)
        static $ch = null;
        if (is_null($ch)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Coinigy App Client; '.php_uname('s').'; PHP/'.phpversion().')');
        }               
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arr);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
        $res = curl_exec($ch);               
      
        if ($res === false)  {
            echo "CURL Failed - Check URL";
            return false;
        }       
       
        $dec = json_decode($res);
       
        if (!$dec) {
           
            echo "Invalid JSON returned - Redirect to Login";
            return false;  
        }               
       
        return $dec;
       
    }
       
   
    private function output_result($result)
    {       
        if($result)
        {
            if(isset($result->error))
                $this->pre($result->error);
            elseif(isset($result))
                $this->pre($result);
        }
    }
   
   
    private function pre($array) {
        echo "<pre>".print_r($array, true)."</pre>";
    }
   
   
}

$foo = new coinigy_api_example_v1();
/*$foo->markets('CPIA');*/

$foo->exchanges();




?>
 

Yaslaw

n/a
Moderator
Wo fragst du deinen Array ab?
Es ist nicht einfache, eine Klasse zu analysieren, die nicht dokumentiert ist und man selber keine Ahnung hat, was die Klasse machen soll.
 

loddarmattheus

Erfahrenes Mitglied
Hallo, die Abfrage der "Exchanges" erfolgt in der Funktion ab Zeile 71:

PHP:
 public function exchanges()
    {
        $post_arr = array();
        $result = $this->doWebRequest('exchanges', $post_arr);     
        $this->output_result($result);               
    }
Daher die Erstellung der Instanz der Klasse in Zeile 303 und Aufruf der Funktion in Zeile 306.

Als Ergebnis liefert mir die Abfrage dann folgende Kurzversion:

Code:
stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [exch_id] => 4
                    [exch_name] => Bitstamp
                    [exch_code] => BITS
                    [exch_fee] => 0.0025
                    [exch_trade_enabled] => 1
                    [exch_balance_enabled] => 1
                    [exch_url] => https://www.bitstamp.net/
                )

            [1] => stdClass Object
                (
                    [exch_id] => 7
                    [exch_name] => Bitfinex
                    [exch_code] => BITF
                    [exch_fee] => 0.003
                    [exch_trade_enabled] => 1
                    [exch_balance_enabled] => 1
                    [exch_url] => https://www.bitfinex.com/
                )
.....
Davon benötige ich jeweils aber nur "exch_name", d.h. es sollte dann hinterher so aussehen:

Bitstamp
Bitfinex
.....

Besser kann ich es jetzt leider auch nicht beschreiben.
 

Yaslaw

n/a
Moderator
Ab PHP5.5 mit array_column()

In output_result() den Autput des Objektes ersetzen
PHP:
$exch_names = array_column($result->data, 'exch_name');
$this->pre($exch_names);
 

loddarmattheus

Erfahrenes Mitglied
Guten Morgen, ich fahre auf meinem System PHP 5.6.1., doch das Einfügen des o.a. Codes in output_result () ab Zeile 285

PHP:
private function output_result($result)
    {       
        if($result)
        {
            if(isset($result->error))
                $this->pre($result->error);
            elseif(isset($result))
                /*$this->pre($result);*/
                $exch_names = array_column($result->data, 'exch_name');
                $this->pre($exch_names);
        }
    }
führt zu folgender leeren Ausgabe:

Array
(
)
 

Yaslaw

n/a
Moderator
Ups, ich sehe gerade. data ist ein Array von Objekten (stdClass Object), nicht von Arrays

Dann musst du es von Hand durchgehen
PHP:
 $exch_names = Array();
foreach($result->data as $obj){
    $exch_names[] = $obj->exch_name;
}
 

loddarmattheus

Erfahrenes Mitglied
Egal, ob ich Deinen Code oben in die Funktion, unten im output_result oder ausserhalb der Klasse einbaue -> er liefert mir kein Ergebnis, nur Fehler-Errors.

P.S. Ich bin leider nicht der PHP-Crack, um Gedankenansätze fortzuführen.
 

loddarmattheus

Erfahrenes Mitglied
Darf ich dich nochmal kurz stören?

Die Ausgabe meines Objects/Arrays sieht so aus:

PHP:
stdClass Object
(
    [data] => stdClass Object
        (
            [exch_code] => GDAX
            [primary_curr_code] => BCH
            [secondary_curr_code] => EUR
            [type] => history
            [history] => Array
                (
                    [0] => stdClass Object
                        (
                            [price] => 723.5600000000
                            [quantity] => 0.2334575900
                            [time_local] => 2018-03-18 13:08:33
                            [type] => BUY
                        )

                    [1] => stdClass Object
                        (
                            [price] => 728.0000000000
                            [quantity] => 0.4706595900
                            [time_local] => 2018-03-18 13:05:01
                            [type] => BUY
                        )

                    [2] => stdClass Object
                        (
                            [price] => 728.0100000000
                            [quantity] => 0.0106529900
                            [time_local] => 2018-03-18 13:05:01
                            [type] => BUY
                        )
Den Zugriff auf die jeweiligen Preise des Arrays history habe ich dank deiner Hilfe hinbekommen mit:

PHP:
$exch_prices = Array();
            foreach($result->data->history as $obj){
            $exch_prices[] = $obj->price;
            }
Doch ich erhalte keinen Zugriff auf die Daten des übergeordneten Objects/Array data?

Trage ich z.B.
PHP:
$exch_headers = array_column($result->data, 'secondary_curr_code');
            echo $exch_headers;
in den Output ein, um mir "EUR" ausgeben zu lassen, kommt die Fehlermeldung: Warning: array_column() expects parameter 1 to be array, object given in.....

Wie kann ich denn auf diese 4 Werte
[exch_code] => GDAX
[primary_curr_code] => BCH
[secondary_curr_code] => EUR
[type] => history

zugreifen?
 

Yaslaw

n/a
Moderator
data ist auch in Objekt und kein Array
PHP:
$obj_vars = get_object_vars($result->data);

foreach ($obj_vars as $name => $value) {
    if(!is_array($value) && !is_object($value))  echo "$name : $value\n";
}
 

loddarmattheus

Erfahrenes Mitglied
Guten Morgen,
deine Lösung ist natürlich viel eleganter als meine:

PHP:
echo "Exch-Code : ".$result->data->exch_code."<br />";
Danke Dir.
 

Yaslaw

n/a
Moderator
Nein, deine ist besser, wenn du weisst was für Elemente vorhanden sind.
Mein ist nur, falls die Elemente nicht bekannt sind.