-3

我正在使用此代码从 coinmarketcap api 导出 3 种货币信息↓:

<?php
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest';
$parameters = [
  'start' => '1',
  'limit' => '3',
  'convert' => 'USD'
];

$headers = [
  'Accepts: application/json',
  'X-CMC_PRO_API_KEY: my_own_key'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL


$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
  CURLOPT_URL => $request,            // set the request URL
  CURLOPT_HTTPHEADER => $headers,     // set the headers 
  CURLOPT_RETURNTRANSFER => 1         // ask for raw response instead of bool
));
header('Content-Type: application/json');
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
echo $response[1]->name;
curl_close($curl); // Close request
?>
-------------

result is ↓:

---------------
stdClass Object
(
    [status] => stdClass Object
        (
            [timestamp] => 2021-06-05T17:35:04.344Z
            [error_code] => 0
            [error_message] => 
            [elapsed] => 12
            [credit_count] => 1
            [notice] => 
            [total_count] => 5530
        )

    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [name] => Bitcoin
                    [symbol] => BTC
                    [slug] => bitcoin
                    [num_market_pairs] => 9702
                    [date_added] => 2013-04-28T00:00:00.000Z
                    [tags] => Array
                        (
                            [0] => mineable
                            [1] => pow
                            [2] => sha-256
                            [3] => store-of-value
                            [4] => state-channels
                            [5] => coinbase-ventures-portfolio
                            [6] => three-arrows-capital-portfolio
                            [7] => polychain-capital-portfolio
                            [8] => binance-labs-portfolio
                            [9] => arrington-xrp-capital
                            [10] => blockchain-capital-portfolio
                            [11] => boostvc-portfolio
                            [12] => cms-holdings-portfolio
                            [13] => dcg-portfolio
                            [14] => dragonfly-capital-portfolio
                            [15] => electric-capital-portfolio
                            [16] => fabric-ventures-portfolio
                            [17] => framework-ventures
                            [18] => galaxy-digital-portfolio
                            [19] => huobi-capital
                            [20] => alameda-research-portfolio
                            [21] => a16z-portfolio
                            [22] => 1confirmation-portfolio
                            [23] => winklevoss-capital
                            [24] => usv-portfolio
                            [25] => placeholder-ventures-portfolio
                            [26] => pantera-capital-portfolio
                            [27] => multicoin-capital-portfolio
                            [28] => paradigm-xzy-screener
                        )

                    [max_supply] => 21000000
                    [circulating_supply] => 18727431
                    [total_supply] => 18727431
                    [platform] => 
                    [cmc_rank] => 1
                    [last_updated] => 2021-06-05T17:34:02.000Z
                    [quote] => stdClass Object
                        (
                            [USD] => stdClass Object
                                (
                                    [price] => 36108.810885716
                                    [volume_24h] => 34482634689.161
                                    [percent_change_1h] => -0.22000049
                                    [percent_change_24h] => -2.95972194
                                    [percent_change_7d] => 5.12257226
                                    [percent_change_30d] => -36.74685465
                                    [percent_change_60d] => -38.11198041
                                    [percent_change_90d] => -29.1772556
                                    [market_cap] => 676225264354.3
                                    [last_updated] => 2021-06-05T17:34:02.000Z
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [id] => 1027
                    [name] => Ethereum
                    [symbol] => ETH
                    [slug] => ethereum
                    [num_market_pairs] => 6019
                    [date_added] => 2015-08-07T00:00:00.000Z
                    [tags] => Array
                        (
                            [0] => mineable
                            [1] => pow
                            [2] => smart-contracts
                            [3] => ethereum
                            [4] => coinbase-ventures-portfolio
                            [5] => three-arrows-capital-portfolio
                            [6] => polychain-capital-portfolio
                            [7] => binance-labs-portfolio
                            [8] => arrington-xrp-capital
                            [9] => blockchain-capital-portfolio
                            [10] => boostvc-portfolio
                            [11] => cms-holdings-portfolio
                            [12] => dcg-portfolio
                            [13] => dragonfly-capital-portfolio
                            [14] => electric-capital-portfolio
                            [15] => fabric-ventures-portfolio
                            [16] => framework-ventures
                            [17] => hashkey-capital-portfolio
                            [18] => kinetic-capital
                            [19] => huobi-capital
                            [20] => alameda-research-portfolio
                            [21] => a16z-portfolio
                            [22] => 1confirmation-portfolio
                            [23] => winklevoss-capital
                            [24] => usv-portfolio
                            [25] => placeholder-ventures-portfolio
                            [26] => pantera-capital-portfolio
                            [27] => multicoin-capital-portfolio
                            [28] => paradigm-xzy-screener
                        )

                    [max_supply] => 
                    [circulating_supply] => 116168370.374
                    [total_supply] => 116168370.374
                    [platform] => 
                    [cmc_rank] => 2
                    [last_updated] => 2021-06-05T17:34:02.000Z
                    [quote] => stdClass Object
                        (
                            [USD] => stdClass Object
                                (
                                    [price] => 2623.0501255974
                                    [volume_24h] => 29873289567.151
                                    [percent_change_1h] => -0.77281419
                                    [percent_change_24h] => -3.22775583
                                    [percent_change_7d] => 13.22095983
                                    [percent_change_30d] => -26.86990803
                                    [percent_change_60d] => 23.92154062
                                    [percent_change_90d] => 56.91903203
                                    [market_cap] => 304715458499.97
                                    [last_updated] => 2021-06-05T17:34:02.000Z
                                )

                        )

                )

            [2] => stdClass Object
                (
                    [id] => 825
                    [name] => Tether
                    [symbol] => USDT
                    [slug] => tether
                    [num_market_pairs] => 13422
                    [date_added] => 2015-02-25T00:00:00.000Z
                    [tags] => Array
                        (
                            [0] => store-of-value
                            [1] => payments
                            [2] => stablecoin
                            [3] => stablecoin-asset-backed
                            [4] => solana-ecosystem
                        )

                    [max_supply] => 
                    [circulating_supply] => 62209255385.452
                    [total_supply] => 64476291004.33
                    [platform] => stdClass Object
                        (
                            [id] => 1027
                            [name] => Ethereum
                            [symbol] => ETH
                            [slug] => ethereum
                            [token_address] => 0xdac17f958d2ee523a2206206994597c13d831ec7
                        )

                    [cmc_rank] => 3
                    [last_updated] => 2021-06-05T17:34:08.000Z
                    [quote] => stdClass Object
                        (
                            [USD] => stdClass Object
                                (
                                    [price] => 1.0004588605114
                                    [volume_24h] => 73072076028.18
                                    [percent_change_1h] => 0.01364608
                                    [percent_change_24h] => -0.08415844
                                    [percent_change_7d] => -0.01154942
                                    [percent_change_30d] => 0.03709874
                                    [percent_change_60d] => -0.09232759
                                    [percent_change_90d] => 0.00764482
                                    [market_cap] => 62237800756.194
                                    [last_updated] => 2021-06-05T17:34:08.000Z
                                )

                        )

                )

        )

)

这个结果是另一个数组中另一个数组中的数组......我无法理解例如:如何回显每个“id”的“价格”

4

1 回答 1

1

$json=json_decode($response,true); echo $json['data']['0']['name'];

最后我失去了时间,但终于找到了答案。

于 2021-06-06T11:09:50.157 回答