How to Get Real-Time Crypto Coin Market Rates in PHP

Are you looking to find real-time crypto coin prices programmatically? Cryptocurrencies have gained immense popularity in recent years. As a result, new products are introduced which provide users real-time exchange rates of cryptocurrencies. In this article, I show you how to get real-time crypto coin prices for your application.

On the internet, you will find a number of services that give details about cryptocurrencies. But, I recommend using coinlayer. There are several benefits of using coinlayer:

  • The coinlayer provides a free plan
  • They provides exchange rate of 385+ cryptocurrencies
  • The coinlayer supports JSONP
  • You will get exchange rate in almost every currencies

Integrating coinlayer into your application is very simple. They provide different endpoints which you need to call and receive a response.

For this tutorial, I am going to use PHP cURL, Guzzle, and vanilla JavaScript to interact with the coinlayer APIs. So, the reader can choose either option as per their project flow.

Getting Started

To get started, you need to register with coinlayer. Users can choose any plan ranging from free to enterprise level.

Once you register with coinlayer, you will get access to a dashboard where you will find your API key. Copy this API key which will be required in the next steps.

Coinlayer API Key

Get Real-Time Crypto Coin Market Prices

The coinlayer APIs are super easy and straightforward to use. Let’s say you need to fetch all crypto coins with their current prices. Using PHP cURL you will write code as follows.

<?php
// set API Access Key
$access_key = 'YOUR_API_KEY';
 
// Initialize CURL:
$ch = curl_init('http://api.coinlayer.com/live?access_key='.$access_key);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
 
// Decode JSON response:
$arr_result = json_decode($json, true);
 
print_r($arr_result);

Make sure you replace the placeholder with the actual API key. Here, you will get all crypto prices in USD format. If you want prices in a different currency then set a target country as an extra parameter. Here is the list of all Target Currencies available with coinlayer. For instance, to fetch prices in Indian currency(INR) we will modify the URL as follows:

$ch = curl_init('http://api.coinlayer.com/live?access_key='.$access_key.'&target=INR');

By default, coinlayer gives a list of all supported cryptocurrencies. You can limit this response by providing specific symbols.

$ch = curl_init('http://api.coinlayer.com/live?access_key='.$access_key.'&symbols=BTC,ETH');

With coinlayer,  you can also fetch currency rates for a specific date. In that case, you need to pass the date in YYYY-mm-dd format as follows.

$ch = curl_init('http://api.coinlayer.com/2018-07-24?access_key='.$access_key);

These are some basic APIs of coinlayer. You can read more about this on their API documentation.

Using Guzzle

The users can also use Guzzle to interact with the coinlayer API. For this, you need to install the Guzzle library using the following command.

composer require guzzlehttp/guzzle

Upon installation, the code to send the API request to coinlayer through Guzzle is as follows.

<?php
require_once "vendor/autoload.php";
 
use GuzzleHttp\Client;
 
// set API Access Key
$access_key = 'YOUR_API_KEY';
 
try {
    $client = new Client([
        // Base URI is used with relative requests
        "base_uri" => "http://api.coinlayer.com",
    ]);
     
    $response = $client->request("GET", "/live", [
        "query" => [
            "access_key" => $access_key,
        ]
    ]);
     
    //get status code using $response->getStatusCode();
 
    $body = $response->getBody();
    $arr_result = json_decode($body, true);
    print_r($arr_result);
} catch(Exception $e) {
    echo $e->getMessage();
}

Using JavaScript

If your application is built using JavaScript, then with the help of Fetch API, you can interact with the coinlayer API as shown below.

var access_key = 'YOUR_API_KEY';
    
fetch('http://api.coinlayer.com/live?access_key=' + access_key)
.then(response => response.json())
.then(data => {
    console.log(data);
});

I hope you understand getting information about crypto coin market rates. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

2 thoughts on “How to Get Real-Time Crypto Coin Market Rates in PHP

Leave a Reply

Your email address will not be published.