PHP Classes

PHP Exchange Rate API Proxy: Provide API to return currency exchange rates

Recommend this page to a friend!
  Info   View files Documentation   View files View files (85)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-10-23 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 47 All time: 10,726 This week: 524Up
Version License PHP version Categories
exchangerate-api 1.0.0Custom (specified...5PHP 5, Web services, Finances
Description 

Author

This package can provide API to return currency exchange rates.

It provides an API that can return information about currencies and exchange rates between them.

The package fetches currency information and exchange rates from other currency APIs and stores the retrieved information in database managed by the API application.

The API provides functions to get et exchange rates for FIAT currencies and crypto-currencies.

Innovation Award
PHP Programming Innovation award nominee
October 2023
Number 6
Many developers are interested in finances and currency exchange rates.

Some services provide information about currencies via their APIs.

This package implements a currency exchange rate that supports FIAT and virtual coin currencies using other currency exchange rate service APIs.

The package uses a database to store the retrieved information to avoid making repeated requests to remote APIs to get the same information within short periods.

This way, it acts as a proxy to make access to the currency exchange rates faster.

Manuel Lemos
Picture of jean paul Delaye
  Performance   Level  
Name: jean paul Delaye <contact>
Classes: 7 packages by
Country: Mexico Mexico
Age: 49
All time rank: 376247 in Mexico Mexico
Week rank: 280 Up4 in Mexico Mexico Up
Innovation award
Innovation award
Nominee: 2x

Documentation

Exchangerate-API

REST API interface delivering fiat currency and crypto exchange rate data.

The REST API interface built with CodeIgniter 4 framework acts as a bridge between your application and external APIs. It fetches exchange rate data from coinmarketcap.com and coingecko.com, storing it in a database for efficient usage of API credits. This caching mechanism reduces the need for frequent API requests, optimizing performance and costs.

The service offers real-time exchange rate data for both fiat currencies and cryptocurrencies, making it a valuable resource for developers, traders, and financial applications. Third-party users can access this data through your API, empowering them to integrate accurate and up-to-date exchange rates into their own applications and services.

Sure, here are the installation steps for setting up the REST API interface for fiat currency and crypto exchange rate data:

  1. Clone or download the repository and upload it to your server or development environment.
  2. Create a `.env` file in the root directory of the project. Add your API keys for coinmarketcap.com and coingecko.com in this file as follows:
    COINMARKETCAP_API_KEY=your_coinmarketcap_api_key
    COINGECKO_API_KEY=your_coingecko_api_key
    
  3. Configure your database credentials in the same `.env` file:
    DATABASE_HOST=your_database_host
    DATABASE_USER=your_database_username
    DATABASE_PASS=your_database_password
    DATABASE_NAME=apiexchangeDB
    
  4. Import the MySQL database structure using the provided `apiexchangeDB.sql` file. This will set up the necessary tables to store the exchange rate data.
    or add this structure to your database:
    CREATE TABLE `cryptocurrency` (
    `id` int(11) NOT NULL,
    `codeID` int(11) NOT NULL,
    `symbol` varchar(10) NOT NULL,
    `price_usd` double NOT NULL,
    `price_mxn` double NOT NULL,
    `percent_change_24h` double NOT NULL,
    `percent_change_1h` double NOT NULL,
    `platform` varchar(20) NOT NULL,
    `token_address` varchar(70) NOT NULL,
    `name` varchar(20) NOT NULL,
    `last_updated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
    
    CREATE TABLE `fiat` ( `id` int(11) NOT NULL, `codeID` int(11) NOT NULL, `symbol` varchar(10) NOT NULL, `price_usd` double NOT NULL, `price_mxn` double NOT NULL, `percent_change_24h` double NOT NULL, `percent_change_1h` double NOT NULL, `platform` varchar(20) NOT NULL, `token_address` varchar(70) NOT NULL, `name` varchar(20) NOT NULL, `last_updated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
  5. Once the database is set up, the API is ready to use. You can start making requests to retrieve fiat currency and crypto exchange rate data.

Please note that it's crucial to keep your .env file secure, as it contains sensitive information like API keys and database credentials. Make sure not to expose it publicly or include it in version control systems.

After following these installation steps, your REST API interface should be up and running, providing access to exchange rate data and serving third-party users with accurate and up-to-date information.

To keep your database updated with the latest exchange rate data and ensure efficient usage of API credits, create a cronjob that points to the API endpoint at localhost/v1/getter/0xdac17f958. Since you are limited to 10,000 credits per month on the free API plan like in coinmarketcap.com, it's recommended to execute the cronjob approximately every 5 minutes.

Here's an example of how to set up the cronjob on a Unix/Linux system:

  1. Open your terminal or SSH into your server.
  2. Edit the crontab file using the following command:
    crontab -e
    
  3. Add a new line to schedule the cronjob. For running the cronjob every 5 minutes, use the following entry:
    /5  * curl http://localhost/v1/getter/0xdac17f958
    
    This command will execute the `curl` request to the specified API endpoint every 5 minutes, updating your database with the latest exchange rate data for the given cryptocurrency (0xdac17f958 in this example).
  4. Save the crontab file and exit the text editor.

With this cronjob in place, your database will be regularly updated with fresh exchange rate data, while also staying within the API credit limits to prevent exceeding the free tier's allowance.

Available Endpoints

The API comes with a number of endpoints where each of them provides different functionality.

To retrieve the latest exchange rate data for all cryptocurrencies. The endpoint is:

   http://rates.intelector.com/v1/cryptocurrencies 

Will deliver a JSON response:

{
    "data": [
        {
            "id": "1",
            "codeID": "1",
            "symbol": "BTC",
            "price_usd": "29763.383835225",
            "price_mxn": "501342.47343414",
            "percent_change_24h": "-0.47498728",
            "percent_change_1h": "-0.21963587",
            "platform": "",
            "token_address": "",
            "name": "Bitcoin",
            "last_updated": "2023-07-20 10:30:01"
        },
        {
            "id": "2",
            "codeID": "52",
            "symbol": "XRP",
            "price_usd": "0.79014423139021",
            "price_mxn": "13.309402772478",
            "percent_change_24h": "-1.07357777",
            "percent_change_1h": "-0.27371891",
            "platform": "",
            "token_address": "",
            "name": "XRP",
            "last_updated": "2023-07-20 10:30:01"
        },
        // Other cryptocurrencies data...
  ]
}

This JSON response will now contain an array of objects, each representing a different cryptocurrency. The objects will include various details like the cryptocurrency symbol, name, price in USD and MXN, percentage changes in price over the last 1 hour, 24 hours, and the timestamp of the last update.


    http://rates.intelector.com/v1/cryptocurrencies/usdt 

Will deliver a JSON response with the latest exchange rate for the specified cryptocurrency (USDT in this case). Here's how the JSON response might look like:

        
 {
    "data": [
        {
            "id": "3",
            "codeID": "825",
            "symbol": "USDT",
            "price_usd": "0.99986625728194",
            "price_mxn": "16.842017201546",
            "percent_change_24h": "-0.01136369",
            "percent_change_1h": "0.00279715",
            "platform": "Ethereum",
            "token_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
            "name": "Tether",
            "last_updated": "2023-07-20 10:30:01"
        }
    ],
    "code": "200"
}


  Files folder image Files  
File Role Description
Files folder imagev1 (11 files, 4 directories)
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation

  Files folder image Files  /  v1  
File Role Description
Files folder imageapp (2 files, 5 directories)
Files folder imagepublic (2 files)
Files folder imagetests (1 file, 4 directories)
Files folder imagewritable (1 file, 4 directories)
  Plain text file .htaccess Data Auxiliary data
  Plain text file builds Data Auxiliary data
  Plain text file composer.json Data Auxiliary data
  Plain text file composer.lock Data Auxiliary data
  Plain text file env Data Auxiliary data
  Plain text file index.php Example Example script
  Plain text file LICENSE Lic. License text
  Plain text file phpunit.xml.dist Data Auxiliary data
  Plain text file preload.php Class Class source
  Plain text file README.md Doc. Documentation
  Plain text file spark Example Example script

  Files folder image Files  /  v1  /  app  
File Role Description
Files folder imageConfig (36 files, 1 directory)
Files folder imageControllers (4 files)
Files folder imageLanguage (1 directory)
Files folder imageModels (2 files)
Files folder imageViews (1 file, 1 directory)
  Plain text file Common.php Aux. Auxiliary script
  HTML file index.html Doc. Documentation

  Files folder image Files  /  v1  /  app  /  Config  
File Role Description
Files folder imageBoot (3 files)
  Plain text file App.php Class Class source
  Plain text file Autoload.php Class Class source
  Plain text file Cache.php Class Class source
  Plain text file Constants.php Aux. Auxiliary script
  Plain text file ContentSecurityPolicy.php Class Class source
  Plain text file Cookie.php Class Class source
  Plain text file CURLRequest.php Class Class source
  Plain text file Database.php Class Class source
  Plain text file DocTypes.php Class Class source
  Plain text file Email.php Class Class source
  Plain text file Encryption.php Class Class source
  Plain text file Events.php Example Example script
  Plain text file Exceptions.php Class Class source
  Plain text file Feature.php Class Class source
  Plain text file Filters.php Class Class source
  Plain text file ForeignCharacters.php Class Class source
  Plain text file Format.php Class Class source
  Plain text file Generators.php Class Class source
  Plain text file Honeypot.php Class Class source
  Plain text file Images.php Class Class source
  Plain text file Kint.php Class Class source
  Plain text file Logger.php Class Class source
  Plain text file Migrations.php Class Class source
  Plain text file Mimes.php Class Class source
  Plain text file Modules.php Class Class source
  Plain text file Pager.php Class Class source
  Plain text file Paths.php Class Class source
  Plain text file Publisher.php Class Class source
  Plain text file Routes.php Example Example script
  Plain text file Security.php Class Class source
  Plain text file Services.php Class Class source
  Plain text file Session.php Class Class source
  Plain text file Toolbar.php Class Class source
  Plain text file UserAgents.php Class Class source
  Plain text file Validation.php Class Class source
  Plain text file View.php Class Class source

  Files folder image Files  /  v1  /  app  /  Config  /  Boot  
File Role Description
  Plain text file development.php Aux. Auxiliary script
  Plain text file production.php Aux. Auxiliary script
  Plain text file testing.php Aux. Auxiliary script

  Files folder image Files  /  v1  /  app  /  Controllers  
File Role Description
  Plain text file BaseController.php Class Class source
  Plain text file Cryptocurrencies.php Class Class source
  Plain text file Getter.php Class Class source
  Plain text file Home.php Class Class source

  Files folder image Files  /  v1  /  app  /  Language  
File Role Description
Files folder imageen (1 file)

  Files folder image Files  /  v1  /  app  /  Language  /  en  
File Role Description
  Plain text file Validation.php Aux. Auxiliary script

  Files folder image Files  /  v1  /  app  /  Models  
File Role Description
  Plain text file BaseModel.php Class Class source
  Plain text file CryptocurrenciesModel.php Class Class source

  Files folder image Files  /  v1  /  app  /  Views  
File Role Description
Files folder imageerrors (2 directories)
  Plain text file welcome_message.php Aux. Auxiliary script

  Files folder image Files  /  v1  /  app  /  Views  /  errors  
File Role Description
Files folder imagecli (3 files)
Files folder imagehtml (5 files)

  Files folder image Files  /  v1  /  app  /  Views  /  errors  /  cli  
File Role Description
  Plain text file error_404.php Aux. Auxiliary script
  Plain text file error_exception.php Example Example script
  Plain text file production.php Aux. Auxiliary script

  Files folder image Files  /  v1  /  app  /  Views  /  errors  /  html  
File Role Description
  Plain text file debug.css Data Auxiliary data
  Plain text file debug.js Data Auxiliary data
  Plain text file error_404.php Aux. Auxiliary script
  Plain text file error_exception.php Example Example script
  Plain text file production.php Aux. Auxiliary script

  Files folder image Files  /  v1  /  public  
File Role Description
  Image file favicon.ico Data Auxiliary data
  Plain text file robots.txt Doc. Documentation

  Files folder image Files  /  v1  /  tests  
File Role Description
Files folder imagedatabase (1 file)
Files folder imagesession (1 file)
Files folder imageunit (1 file)
Files folder image_support (3 directories)
  Plain text file README.md Doc. Documentation

  Files folder image Files  /  v1  /  tests  /  database  
File Role Description
  Plain text file ExampleDatabaseTest.php Class Class source

  Files folder image Files  /  v1  /  tests  /  session  
File Role Description
  Plain text file ExampleSessionTest.php Class Class source

  Files folder image Files  /  v1  /  tests  /  unit  
File Role Description
  Plain text file HealthTest.php Class Class source

  Files folder image Files  /  v1  /  tests  /  _support  
File Role Description
Files folder imageDatabase (2 directories)
Files folder imageLibraries (1 file)
Files folder imageModels (1 file)

  Files folder image Files  /  v1  /  tests  /  _support  /  Database  
File Role Description
Files folder imageMigrations (1 file)
Files folder imageSeeds (1 file)

  Files folder image Files  /  v1  /  tests  /  _support  /  Database  /  Migrations  
File Role Description
  Plain text file 2020-02-22-222222_example_migration.php Class Class source

  Files folder image Files  /  v1  /  tests  /  _support  /  Database  /  Seeds  
File Role Description
  Plain text file ExampleSeeder.php Class Class source

  Files folder image Files  /  v1  /  tests  /  _support  /  Libraries  
File Role Description
  Plain text file ConfigReader.php Class Class source

  Files folder image Files  /  v1  /  tests  /  _support  /  Models  
File Role Description
  Plain text file ExampleModel.php Class Class source

  Files folder image Files  /  v1  /  writable  
File Role Description
Files folder imagecache (1 file)
Files folder imagelogs (1 file)
Files folder imagesession (1 file)
Files folder imageuploads (1 file)
  Plain text file .htaccess Data Auxiliary data

  Files folder image Files  /  v1  /  writable  /  cache  
File Role Description
  HTML file index.html Doc. Documentation

  Files folder image Files  /  v1  /  writable  /  logs  
File Role Description
  HTML file index.html Doc. Documentation

  Files folder image Files  /  v1  /  writable  /  session  
File Role Description
  HTML file index.html Doc. Documentation

  Files folder image Files  /  v1  /  writable  /  uploads  
File Role Description
  HTML file index.html Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:47
This week:0
All time:10,726
This week:524Up