无法访问stdClass数组值(PHP)

时间:2016-07-10 08:12:54

标签: php arrays api object stdclass

我试图从返回的stdClass数组中获取'Limit'的值。 我得到的只是无效的访问错误。 无效的对象,无效的数组偏移量

以下是2次单独尝试

代码:

function callbittrex($url, $flags, $apikey, $apisecret){
        $nonce=time();
        if($flags == 0){
                $uri = $url.'?apikey='.$apikey.'&nonce='.$nonce;
        }
        if($flags == 1){
                $uri = $url;
        }
        $sign=hash_hmac('sha512',$uri,$apisecret);
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $execResult = curl_exec($ch);
        curl_close($ch);
        $obj = json_decode($execResult);
        return $obj;

}
$flags = 0;
$url='https://bittrex.com/api/v1.1/market/getopenorders';
$myobj = callbittrex($url, $flags, $apikey, $apisecret);
echo $myobj->result['0']->Limit;

错误:

PHP Notice:  Undefined property: stdClass::$result in /home/buycoinz.php on line 31

Notice: Undefined property: stdClass::$result in /home/buycoinz.php on line 31
PHP Notice:  Trying to get property of non-object in /home/buycoinz.php on line 31

Notice: Trying to get property of non-object in /home/buycoinz.php on line 31

更新:更正了拼写错误“结果”。

返回数据:

stdClass::__set_state(array(
   'success' => true,
   'message' => '',
   'result' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'Uuid' => NULL,
       'OrderUuid' => 'd880ed74-9da8-4725-8b4d-a02daa2d3a66',
       'Exchange' => 'BTC-EXP',
       'OrderType' => 'LIMIT_BUY',
       'Quantity' => 2,
       'QuantityRemaining' => 2,
       'Limit' => 0.00053649999999999998,
       'CommissionPaid' => 0,
       'Price' => 0,
       'PricePerUnit' => NULL,
       'Opened' => '2016-07-06T12:12:37.377',
       'Closed' => NULL,
       'CancelInitiated' => false,
       'ImmediateOrCancel' => false,
       'IsConditional' => false,
       'Condition' => 'NONE',
       'ConditionTarget' => NULL,
    )),
    1 => 
    stdClass::__set_state(array(
       'Uuid' => NULL,
       'OrderUuid' => '968bdd76-5cb2-4f80-9126-bc8fe6414e8a',
       'Exchange' => 'BTC-EXP',
       'OrderType' => 'LIMIT_BUY',
       'Quantity' => 2,
       'QuantityRemaining' => 2,
       'Limit' => 0.00050000000000000001,
       'CommissionPaid' => 0,
       'Price' => 0,
       'PricePerUnit' => NULL,
       'Opened' => '2016-07-06T12:14:47.91',
       'Closed' => NULL,
       'CancelInitiated' => false,
       'ImmediateOrCancel' => false,
       'IsConditional' => false,
       'Condition' => 'NONE',
       'ConditionTarget' => NULL,
    )),
  ),
))

第二次尝试:

代码:

function callbittrex($url, $flags, $apikey, $apisecret){
        $nonce=time();
        if($flags == 0){
                $uri = $url.'?apikey='.$apikey.'&nonce='.$nonce;
        }
        if($flags == 1){
                $uri = $url;
        }
        $sign=hash_hmac('sha512',$uri,$apisecret);
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $execResult = curl_exec($ch);
        curl_close($ch);
        $obj = json_decode($execResult, true);
        return $obj;

}
$flags = 0;

$url='https://bittrex.com/api/v1.1/market/getopenorders';
$myobj = callbittrex($url, $flags, $apikey, $apisecret);
foreach ($myobj as $dirobj){
        var_dump( $dirobj['array'][0] );
                //echo $dirobj['Limit'];
}

错误:

NULL
PHP Warning:  Illegal string offset 'array' in /home/buycoinz.php on line 31

Warning: Illegal string offset 'array' in /home/buycoinz.php on line 31
PHP Notice:  Uninitialized string offset: 0 in /home/buycoinz.php on line 31

在foreach()循环后输出,stdClass数组显示为禁用:

bool(true)
string(0) ""
array(2) {
  [0]=>
  array(17) {
    ["Uuid"]=>
    NULL
    ["OrderUuid"]=>
    string(36) "d880ed74-9da8-4725-8b4d-a02daa2d3a66"
    ["Exchange"]=>
    string(7) "BTC-EXP"
    ["OrderType"]=>
    string(9) "LIMIT_BUY"
    ["Quantity"]=>
    float(2)
    ["QuantityRemaining"]=>
    float(2)
    ["Limit"]=>
    float(0.0005365)
    ["CommissionPaid"]=>
    float(0)
    ["Price"]=>
    float(0)
    ["PricePerUnit"]=>
    NULL
    ["Opened"]=>
    string(23) "2016-07-06T12:12:37.377"
    ["Closed"]=>
    NULL
    ["CancelInitiated"]=>
    bool(false)
    ["ImmediateOrCancel"]=>
    bool(false)
    ["IsConditional"]=>
    bool(false)
    ["Condition"]=>
    string(4) "NONE"
    ["ConditionTarget"]=>
    NULL
  }
  [1]=>
  array(17) {
    ["Uuid"]=>
    NULL
    ["OrderUuid"]=>
    string(36) "968bdd76-5cb2-4f80-9126-bc8fe6414e8a"
    ["Exchange"]=>
    string(7) "BTC-EXP"
    ["OrderType"]=>
    string(9) "LIMIT_BUY"
    ["Quantity"]=>
    float(2)
    ["QuantityRemaining"]=>
    float(2)
    ["Limit"]=>
    float(0.0005)
    ["CommissionPaid"]=>
    float(0)
    ["Price"]=>
    float(0)
    ["PricePerUnit"]=>
    NULL
    ["Opened"]=>
    string(22) "2016-07-06T12:14:47.91"
    ["Closed"]=>
    NULL
    ["CancelInitiated"]=>
    bool(false)
    ["ImmediateOrCancel"]=>
    bool(false)
    ["IsConditional"]=>
    bool(false)
    ["Condition"]=>
    string(4) "NONE"
    ["ConditionTarget"]=>
    NULL
  }
}

如何在此stdClass数组中访问'Limit'的值?

1 个答案:

答案 0 :(得分:0)

在第一次尝试中,您试图获得结果 s 而不是结果

在第二次尝试中(当你实际返回数组,而不是stdClass)时,你可以简单地得到请求结果:

$result = callbittrex($url, $flags, $apikey, $apisecret)['result'];