My question is about usage of Binance API on WordPress 👇

Hello!
I am a very beginner, but I understand the world of PHP script. I already have my Binance API Key and Secret Key. By definition, I don’t share these. I’m just a beginner, but I’m not that stupid :wink:

:white_check_mark: My question is the following:
I manage PHP codes in the content of posts and pages in the WordPress CMS with the PHPCode Snippets solution.

Can you tell me how to get a list of top 100 cryptocurrencies by market cap? Please provide a PHP code that contains the location of your Binance API key and secret key. I would need a complete source code, which if I put it in the mentioned PHPCode Snippets, I could easily use it with a simple [shortcode] call.

Or is it not that simple? Would WordPress need something else for this?

Thank you so much

1 Like
<?php
// Define your Binance API Key and Secret Key here
define('BINANCE_API_KEY', 'YOUR_API_KEY');
define('BINANCE_SECRET_KEY', 'YOUR_SECRET_KEY');

function getTop100Cryptos() {
    $apiUrl = 'https://api.binance.com/api/v3/ticker/24hr';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $apiUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // Set headers with API Key
    $headers = [
        'X-MBX-APIKEY: ' . BINANCE_API_KEY,
    ];
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($curl);
    curl_close($curl);

    $data = json_decode($result, true);
    usort($data, function($a, $b) {
        return $b['quoteVolume'] - $a['quoteVolume'];
    });

    return array_slice($data, 0, 100);
}

function displayTop100Cryptos() {
    $cryptos = getTop100Cryptos();
    echo '<ul>';
    foreach ($cryptos as $crypto) {
        echo '<li>' . htmlspecialchars($crypto['symbol']) . ' - Trading Volume: ' . htmlspecialchars($crypto['quoteVolume']) . '</li>';
    }
    echo '</ul>';
}

add_shortcode('top_100_cryptos', 'displayTop100Cryptos');
?>

The code sorts the cryptocurrencies by their 24-hour trading volume, not by market cap, as the Binance API doesn’t directly provide market cap data.
This code uses PHP’s curl functions for HTTP requests. Ensure your server supports curl and it supports executing PHP code through snippets.

1 Like