Ethereum: Bitcoin real-time buy and sell price API

Ethereum Real-Time Buy and Sell Price API for PHP

As a popular cryptocurrency, Ethereum has become an essential component of any online project or application. To provide real-time updates on the Bitcoin-USD (BTC-USDT) price, we’ll be using the official [CoinGecko API]( This article will guide you through setting up a PHP script to fetch and display Ethereum’s real-time buy and sell prices.

Prerequisites

Before diving into the code, ensure you have:

  • A working PHP server with Apache or Nginx installed.

  • The CoinGecko API credentials set up in your environment (API key, secret key, and base URL).

  • A basic understanding of PHP, JSON, and SQL.

Step 1: Create a new PHP script

Create a new file, e.g., ethereum_api.php, with the following code:

// Set API credentials

$api_key = 'YOUR_API_KEY';

$api_secret = 'YOUR_API_SECRET';

$base_url = '

// Set your database connection

$db_host = 'your_host_name';

$db_username = 'your_database_username';

$db_password = 'your_database_password';

$db_name = 'ethereum_db'; // Replace with your database name

// Function to fetch real-time data from CoinGecko API

function get_price($coin) {

$url = "$base_url/v3/coins/$coin/price";

$headers = array(

'X-API-KEY: '.$api_key,

'X-API-SECRET: '.$api_secret,

'Content-Type: application/json'

);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POST, false);

$response = curl_exec($ch);

curl_close($ch);

return json_decode($response, true);

}

// Function to display real-time data on your site

function display_price($coin) {

$price = get_price($coin);

echo '

Real-Time Price of $coin





';

// Display price in a readable format (e.g., USD)

$price_in_usd = $price['current_price'];

echo 'Current price: $price_in_usd';

}

// Example usage

$coins = array('ethereum', 'bitcoin');

foreach ($coins as $coin) {

display_price($coin);

}

Step 2: Set up your database

Create a new file, e.g., ethereum_db.sql, to set up your MySQL database:

CREATE DATABASE ethereum_db;

USE ethereum_db;

CREATE TABLE prices (

id INT AUTO_INCREMENT PRIMARY KEY,

coin VARCHAR(255),

price DECIMAL(10, 2) DEFAULT NULL

);

Step 3: Configure your PHP script

Update the ethereum_api.php file to include your database connection details:

“`php

$db_host = ‘your_host_name’;

$db_username = ‘your_database_username’;

$db_password = ‘your_database_password’;

$db_name = ‘ethereum_db’; // Replace with your database name

// Function to fetch real-time data from CoinGecko API

function get_price($coin) {

$url = “$base_url/v3/coins/$coin/price”;

$headers = array(

‘X-API-KEY: ‘.$api_key,

‘X-API-SECRET: ‘.$api_secret,

‘Content-Type: application/json’

);

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POST, false);

$response = curl_exec($ch);

curl_close($ch);

return json_decode($response, true);

}

// Function to display real-time data on your site

function display_price($coin) {

$price = get_price($coin);

echo ‘

Real-Time Price of $coin

‘;

// Display price in a readable format (e.g.

Leave a Comment