在PHP中从OKX API响应中提取数据[已关闭]

oo7oh9g9  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(133)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

昨天关门了。
Improve this question
我得到了一个数组的结果:

array(3) { 
    ["code"]=> string(1) "0" 
    ["data"]=> array(1) { 
        [0]=> array(10) { 
            ["adjEq"]=> string(0) "" 
            ["details"]=> array(3) { 
                [0]=> array(25) { 
                    ["availBal"]=> string(17) "1306.166306013251" 
                    ["availEq"]=> string(17) "1306.166306013251" 
                    ["cashBal"]=> string(17) "1662.567065013253" 
                    ["ccy"]=> string(4) "USDT" 
                    ["crossLiab"]=> string(0) "" 
                    ["disEq"]=> string(18) "1502.2602611547543" 
                    ["eq"]=> string(17) "1501.869775013251" 
                    ["eqUsd"]=> string(18) "1502.2602611547543" 
                    ["fixedBal"]=> string(1) "0" 
                    ["frozenBal"]=> string(10) "195.703469" 
                    ["interest"]=> string(0) "" 
                    ["isoEq"]=> string(1) "0" 
                    ["isoLiab"]=> string(0) "" 
                    ["isoUpl"]=> string(1) "0" 
                    ["liab"]=> string(0) "" 
                    ["maxLoan"]=> string(0) "" 
                    ["mgnRatio"]=> string(16) "61.6659053967789" 
                    ["notionalLever"]=> string(17) "2.158184040937505" 
                    ["ordFrozen"]=> string(1) "0" 
                    ["spotInUseAmt"]=> string(0) "" 
                    ["stgyEq"]=> string(1) "0" 
                    ["twap"]=> string(1) "0" 
                    ["uTime"]=> string(13) "1685067331617" 
                    ["upl"]=> string(19) "-160.69729000000203" 
                    ["uplLiab"]=> string(0) "" 
                } 
                [1]=> array(25) { 
                    ["availBal"]=> string(7) "0.01998" 
                    ["availEq"]=> string(7) "0.01998" 
                    ["cashBal"]=> string(7) "0.01998" 
                    ["ccy"]=> string(3) "LTC" 
                    ["crossLiab"]=> string(0) "" 
                    ["disEq"]=> string(10) "1.64622213" 
                    ["eq"]=> string(7) "0.01998" 
                    ["eqUsd"]=> string(18) "1.7328654000000001" 
                    ["fixedBal"]=> string(1) "0" 
                    ["frozenBal"]=> string(1) "0" 
                    ["interest"]=> string(0) "" 
                    ["isoEq"]=> string(1) "0" 
                    ["isoLiab"]=> string(0) "" 
                    ["isoUpl"]=> string(1) "0" 
                    ["liab"]=> string(0) "" 
                    ["maxLoan"]=> string(0) "" 
                    ["mgnRatio"]=> string(0) "" 
                    ["notionalLever"]=> string(1) "0" 
                    ["ordFrozen"]=> string(1) "0" 
                    ["spotInUseAmt"]=> string(0) "" 
                    ["stgyEq"]=> string(1) "0" 
                    ["twap"]=> string(1) "0" 
                    ["uTime"]=> string(13) "1683772592570" 
                    ["upl"]=> string(1) "0" 
                    ["uplLiab"]=> string(0) "" 
                } 
                [2]=> array(25) { 
                    ["availBal"]=> string(12) "13.769813403" 
                    ["availEq"]=> string(12) "13.769813403" 
                    ["cashBal"]=> string(12) "13.769813403" 
                    ["ccy"]=> string(4) "DOGE" 
                    ["crossLiab"]=> string(0) "" 
                    ["disEq"]=> string(17) "0.876792868436025" 
                    ["eq"]=> string(12) "13.769813403" 
                    ["eqUsd"]=> string(18) "0.9742142982622499" 
                    ["fixedBal"]=> string(1) "0" 
                    ["frozenBal"]=> string(1) "0" 
                    ["interest"]=> string(0) "" 
                    ["isoEq"]=> string(1) "0" 
                    ["isoLiab"]=> string(0) "" 
                    ["isoUpl"]=> string(1) "0" 
                    ["liab"]=> string(0) "" 
                    ["maxLoan"]=> string(0) "" 
                    ["mgnRatio"]=> string(0) "" 
                    ["notionalLever"]=> string(1) "0" 
                    ["ordFrozen"]=> string(1) "0" 
                    ["spotInUseAmt"]=> string(0) "" 
                    ["stgyEq"]=> string(1) "0" 
                    ["twap"]=> string(1) "0" 
                    ["uTime"]=> string(13) "1683772167400" 
                    ["upl"]=> string(1) "0" 
                    ["uplLiab"]=> string(0) "" 
                } 
            } 
            ["imr"]=> string(0) "" 
            ["isoEq"]=> string(1) "0" 
            ["mgnRatio"]=> string(0) "" 
            ["mmr"]=> string(0) "" 
            ["notionalUsd"]=> string(0) "" 
            ["ordFroz"]=> string(0) "" 
            ["totalEq"]=> string(18) "1504.9673408530166" 
            ["uTime"]=> string(13) "1685072984612" 
        } 
    } 
    ["msg"]=> string(0) "" 
}

我想知道的是如何把它作为一个单独的变量取出来??
有人能帮我吗?
找了两天了,还是没有结果

kr98yfug

kr98yfug1#

我认为你首先需要使用像curlfile_get_contents这样的函数向API端点发出请求,或者像Guzzle这样的PHP包可以随意使用任何一个,正如我看到的那样,你已经得到了响应,所以然后使用json_decode函数将其解码为JSON并访问数据。
下面是一个示例代码片段,演示了如何在PHP中从OKEx API响应中提取数据:

// Set up API request
$apiKey = 'your-api-key';
$apiSecret = 'your-api-secret';
$endpoint = 'https://www.okex.com/api/v5/account/balance';
$timestamp = time();
$signString = $timestamp . 'GET/api/v5/account/balance';
$signature = hash_hmac('sha256', $signString, $apiSecret);

// Set up cURL request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'OK-ACCESS-KEY: ' . $apiKey,
    'OK-ACCESS-SIGN: ' . $signature,
    'OK-ACCESS-TIMESTAMP: ' . $timestamp,
    'OK-ACCESS-PASSPHRASE: your-passphrase'
]);

// Execute cURL request and decode response
$response = curl_exec($curl);
$data = json_decode($response, true);
curl_close($curl);

// Extract data from response
if (isset($data['code']) && $data['code'] === 0) {
    $balances = $data['data'];
    foreach ($balances as $balance) {
        echo $balance['currency'] . ': ' . $balance['total'] . PHP_EOL;
    }
} else {
    echo 'Error: ' . $data['msg'] . PHP_EOL;
}

请注意,您需要修改代码以匹配您请求的特定API端点和您想要从响应中提取的数据,但您必须查看将foreach放置在何处以及如何保护它,因为响应并不总是相同的。

相关问题