0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CoinGeckoAPIをWordPressで活用|価格変動の大きい仮想通貨を表示する実装手順

Last updated at Posted at 2025-05-01
  • WordPress
  • PHP
  • CoinGecko API
  • 仮想通貨

Posted at 2024-05-15

こんにちは、仮想通貨開発者の皆さん。仮想通貨総合研究所シニアブロックチェーンエンジニアのSatoshi Nakamotoです。今回は、無料で利用できるCoinGecko APIを使って、WordPressサイトに価格変動の大きい仮想通貨を表示する方法をご紹介します。

CoinGecko APIの概要

CoinGecko APIは、13,000以上の仮想通貨の価格データにアクセスできる人気のAPIです。無料プランでは以下の機能が利用可能です:

  • 1分あたり10-30リクエスト
  • 現在の価格データ取得
  • 24時間の価格変動率
  • 取引高データ
  • 時価総額情報

API利用開始手順

  1. APIキーの取得は不要
  2. エンドポイント: https://api.coingecko.com/api/v3/
  3. レート制限: 無料プランで10-30 calls/minute

WordPress実装例

functions.phpに以下のコードを追加します:

function get_volatile_crypto() {
    $endpoint = 'https://api.coingecko.com/api/v3/simple/price';
    $params = array(
        'ids' => 'bitcoin,ethereum,binancecoin',
        'vs_currencies' => 'usd',
        'include_24hr_change' => 'true'
    );
    
    $url = add_query_arg($params, $endpoint);
    $response = wp_remote_get($url);
    
    if (is_wp_error($response)) {
        return false;
    }
    
    $data = json_decode(wp_remote_retrieve_body($response), true);
    return $data;
}

// ショートコードの登録
add_shortcode('volatile_crypto', 'display_volatile_crypto');

function display_volatile_crypto() {
    $crypto_data = get_volatile_crypto();
    if (!$crypto_data) {
        return 'データを取得できませんでした';
    }
    
    $output = '<div class="crypto-prices">';
    foreach ($crypto_data as $coin => $data) {
        $change = round($data['usd_24h_change'], 2);
        $price = number_format($data['usd'], 2);
        
        $output .= sprintf(
            '<div class="coin %s">
                <h3>%s</h3>
                <p>価格: $%s</p>
                <p>変動率: %s%%</p>
            </div>',
            $change >= 0 ? 'positive' : 'negative',
            ucfirst($coin),
            $price,
            $change
        );
    }
    $output .= '</div>';
    
    return $output;
}

使用方法

  1. 上記コードをfunctions.phpに追加
  2. 投稿や固定ページに [volatile_crypto] ショートコードを挿入
  3. 必要に応じてCSSでスタイリング

スタイリング例

.crypto-prices {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
}

.coin {
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    min-width: 200px;
}

.positive { background: #e8f5e9; }
.negative { background: #ffebee; }

注意点

  • APIのレート制限に注意(キャッシュの実装推奨)
  • 本番環境ではエラーハンドリングを強化
  • 価格データの更新頻度は要件に応じて調整

このコードをベースに、表示する通貨の追加や、更新頻度の調整など、必要に応じてカスタマイズしてください。

CoinGecko API Documentation

参考元: CoinGecko API Documentation

仮想通貨総合研究所|仮想通貨技術の研究開発サイト

仮想通貨総合研究所は、仮想通貨技術を活用して社会課題の解決に取り組む研究開発機関です。

仮想通貨技術の研究開発経験が豊富な専門家チームが、最新の仮想通貨技術動向や、革新的なブロックチェーンソリューションを紹介します。

取引ツール開発や技術導入支援、また仮想通貨に関する情報配信など、幅広いサービスを展開しています。

主なサービス:

  • 仮想通貨取引支援ツールの開発・提供
  • 仮想通貨技術の導入開発支援
  • 仮想通貨の投資情報配信
  • 仮想通貨の時価総額リスト提供
  • 取引所比較情報の提供
  • YouTube公式チャンネルでの市場分析配信

また、ブロックチェーン技術を活用した様々なソリューションも提供しているので、革新的な技術導入をお考えの方はぜひご相談ください。

仮想通貨総合研究所 |仮想通貨技術の研究開発サイト

仮想通貨の時価総額と価格 | 仮想通貨時価総額ランキング

仮想通貨取引所リスト | 取引所比較・ランキングサイト

仮想通貨総合研究所公式YouTube | 市場分析・投資情報チャンネル

[参考元: https://crypto-soken.co.jp/]

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?