Live Ethereum Buy and Sell Prices API for PHP
Being a popular cryptocurrency, Ethereum has become an integral component of any online project or application. We will use the official [CoinGecko API]( to provide real-time updates on the Bitcoin-USD (BTC-USDT) price. In this article, you will learn how to configure a PHP script to receive and display real-time Ethereum buy and sell prices.
Prerequisites
Before you start learning the code, make sure you have:
- A working PHP server with Apache or Nginx installed.
- CoinGecko API credentials configured in your environment (API Key, Secret Key, and Base URL).
- Basic knowledge of PHP, JSON and SQL.
Step 1: Create a new PHP script
Create a new file, for example ethereum_api.php
, with the following code:
// Set API credentials
$api_key = 'YOUR_API_KEY';
$api_secret = 'YOUR_API_SECRET';
$base_url = '
// Establish a connection to the database
$db_host = 'your_host_name';
$db_username = 'your_database_username';
$db_password = 'your_database_password';
$db_name = 'ethereum_db'; // Replace with your database name
// A function for extracting real-time data from the 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_HTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Function to display data in real time on your site
function display_price($coin) {
$price = get_price($coin);
echo '
Price of $coin
in real time';
// Display the price in a readable format (e.g. USD)
$price_in_US$ = $price['current_price'];
echo 'Current price: $price_in_usd';
}
// Usage example
$coins = array('ethereum', 'bitcoin');
foreach ($coins as $coin) {
display_price($coin);
}
Step 2: Set Up Your Database
Create a new file, for example ethereum_db.sql
, to configure the MySQL database:
CREATE DATABASE ethereum_db;
USE ethereum_db;
CREATE PRICE TABLE (
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 the database connection information:
“`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
// A function for extracting real-time data from the 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_HTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Function to display data in real time on your site
function display_price($coin) {
$price = get_price($coin);
echo ‘
Price of $coin
in real time’;
// Display the price in a readable format (for example,