A simple package for interacting with the exchangerate.host API

Overview

PHP Exchange Rates

Latest Version on Packagist Build Status Total Downloads PHP from Packagist GitHub license

Table of Contents

Overview

Exchange Rates is a simple PHP package used for interacting with the exchangerate.host API. You can use it to get the latest or historical exchange rates and convert monetary values between different currencies and cryptocurrencies.

Installation

You can install the package via Composer:

composer require ashallendesign/exchange-rates

The package has been developed and tested to work with the following minimum requirements:

  • PHP 8.0

Usage

Methods

Available Currencies

To get the available currencies that are supported by the package, you can use the ->currencies() method like so:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$exchangeRates->currencies();

Exchange Rate

Getting the Rate Between Two Currencies

To get the exchange for one currency to another, you can use the ->exchangeRate() method.

The example below shows how to get the exchange rate from 'GBP' to 'EUR' for today:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->exchangeRate('GBP', 'EUR');

// $result: '1.10086'

Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.

Getting the Rate Between More Than Two Currencies

It is possible to get the exchange rates for multiple currencies in one call. This can be particularly useful if you are needing to get many exchange rates at once and do not want to make multiple API calls.

To do this, you can use ->exchangeRate() method and pass an array of currency code strings as the second parameter. This will return an array containing the exchange rates as strings.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' and 'USD' for today.

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->exchangeRate('GBP', ['EUR', 'USD']);

// $result: [
//     'EUR' => '1.10086',
//     'USD' => '1.25622'
// ];

Exchange Rates Between Date Range

Getting the Rates Between Two Currencies

To get the exchange rates between two currencies between a given date range, you can use the ->exchangeRateBetweenDateRange() method.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' for the past 3 days:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->exchangeRateBetweenDateRange(
    'GBP',
    'EUR',
    Carbon::now()->subWeek(),
    Carbon::now()
);

// $result: [
//     '2020-07-07' => [
//         'EUR' => '1.1092623405',
//     ],
//     '2020-07-08' => [
//         'EUR' => '1.1120625424',
//     ],
//     '2020-07-09' => [
//         'EUR' => '1.1153867604',
//     ],
// ];
Getting the Rates Between More Than Two Currencies

To get the exchange rates for multiple currencies in one call, you can pass an array of currency codes strings as the second parameter to the ->exchangeRateBetweenDateRange() method.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' and 'USD' for the past 3 days:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->exchangeRateBetweenDateRange(
    'GBP',
    ['EUR', 'USD'],
    Carbon::now()->subDays(3),
    Carbon::now(),
);

// $result: [
//     '2020-07-07' => [
//         'EUR' => '1.1092623405',
//         'USD' => '1.2523571825',
//      ],
//     '2020-07-08' => [
//         'EUR' => '1.1120625424',
//         'USD' => '1.2550737853',
//      ],
//     '2020-07-09' => [
//         'EUR' => '1.1153867604',
//         'USD' => '1.2650716636',
//      ],
// ];

Convert Currencies

When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).

Converting Between Two Currencies

Similar to how you can get the exchange rate from one currency to another, you can also convert a monetary value from one currency to another. To do this you can use the ->convert() method.

The example below shows how to convert £1 'GBP' to 'EUR' at today's exchange rate:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->convert(100, 'GBP', 'EUR');

// $result: '110.15884906'

Note: If a Carbon date object is passed as the third parameter, the exchange rate for that day will be used (if valid). If no date is passed, today's exchange rate will be used.

Converting Between More Than Two Currencies

You can also use the ->convert() method to convert a monetary value from one currency to multiple currencies. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at today's exchange rate:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$result = $exchangeRates->convert(
    100,
    'GBP',
    ['EUR', 'USD'],
    Carbon::now(),
);

// $result: [
//     'EUR' => '110.15884906',
//     'USD' => '125.30569081'
// ];

Convert Currencies Between Date Range

When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).

Converting Between Two Currencies in a Date Range

Similar to getting the exchange rates between a date range, you can also get convert monetary values from one currency to another using the exchange rates. To do this you can use the ->convertBetweenDateRange() method.

The example below shows how to convert £1 'GBP' to 'EUR' using the exchange rates for the past 3 days:

$exchangeRates = new \AshAllenDesign\ExchangeRates\Classes\ExchangeRate();

$exchangeRates->convertBetweenDateRange(
    100,
    'GBP',
    'EUR',
    Carbon::now()->subDays(3),
    Carbon::now()
);

// $result: [
//     '2020-07-07' => [
//         'EUR' => '110.92623405',
//      ],
//     '2020-07-08' => [
//         'EUR' => '111.20625424',
//      ],
//     '2020-07-09' => [
//         'EUR' => '111.53867604',
//      ],
// ];
Converting Between More Than Two Currencies in a Date Range

You can also use the ->convertBetweenDateRange() method to convert a monetary value from one currency to multiple currencies using the exchange rates between a date range. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at the past three days' exchange rates:

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', ['EUR', 'USD'], Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => [
//         'EUR' => '110.92623405',
//         'USD' => '125.23571825',
//      ],
//     '2020-07-08' => [
//         'EUR' => '111.20625424',
//         'USD' => '125.50737853',
//      ],
//     '2020-07-09' => [
//         'EUR' => '111.53867604',
//         'USD' => '126.50716636',
//      ],
// ];

Testing

To run the tests for the package, you can use the following command:

composer test

Security

If you find any security related issues, please contact me directly at [email protected] to report it.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this library, please use the following guidelines before submitting your pull request:

  • Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
  • Follow PSR-12 coding standards.
  • Make all pull requests to the master branch.

Credits

Changelog

Check the CHANGELOG to get more information about the latest changes.

Upgrading

Check the UPGRADE guide to get more information on how to update this library to newer versions.

License

The MIT License (MIT). Please see License File for more information.

You might also like...
This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube features.

Laravel Youtube Client This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube featu

Bastion API is a simple API made to get query informations

Bastion API is a simple API made to get query informations

Simple Symfony API-Platform Template which you can use to start to develop with symfony and api-platform

symfony-api-platform-skeleton Simple Template for Symfony API You can fork it and change the git remote to your Repo git remote set-url your-git-remo

Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics (functional) API that doesn't cause vendor lock-in.

Metrics Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics API that doesn't cau

Traits used primarily in the v6 package but also available as a helper package for applications

Phalcon Traits This package contains traits with methods that are used for Phalcon v6 onward. It can also be useful to others that want short snippets

This package is aimed to be a simplistic PHP workflow package that works in a similar fashion to GitHub Actions.

Workflow This package is aimed to be a simplistic PHP workflow package that works in a similar fashion to GitHub Actions. Installation To install this

Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.

Laravel Blog Have you worked with Wordpress? Developers call this package wordpress-like laravel blog. Give our package a Star to support us ⭐ 😍 Inst

Quick package/plugin/component (repo) lookup for your favourite package managers
Quick package/plugin/component (repo) lookup for your favourite package managers

Package Managers (Download latest release) Package Repo Search Quick package/plugin/component (repo) lookup for your favourite package managers.

A PHP package to make the Chronopost API easier to use.

Chronopost API A PHP package to make the Chronopost API easier to use. Table of Contents Requirements Installation Usage Testing Requirements PHP 7.3,

Comments
  • Migrating PHPUnit setting and using assertSame

    Migrating PHPUnit setting and using assertSame

    Changed log

    • Since this PHP package supports PHPUnit 9 version, it can be good to migrate PHPUnit setting to new format.
    • Using the assertSame to make assertion equals strict.
    opened by peter279k 1
Releases(v1.0.0)
Owner
Ash Allen
Laravel Web Developer Preston, United Kingdom
Ash Allen
🎮 A simple command line tool for installing and interacting with your leaf apps

Leaf CLI 2 A simple command line tool for creating and interacting with your leaf projects. You can do stuff like installing packages, interacting wit

Leaf Framework 7 Aug 24, 2022
PHP library for interacting with the Waves blockchain.

Waves-PHP PHP client library for interacting with Waves blockchain platform. Installation composer require waves/client Usage See example.php for full

Waves Protocol 4 Sep 30, 2022
A fluent interface for interacting with Netopia's services.

laravel-netopia A fluent interface for interacting with Netopia's services. Info Database It'll create a table named netopia_payments with the followi

Codestage 3 Oct 10, 2022
Repman - PHP Repository Manager: packagist proxy and host for private packages

Repman - PHP Repository Manager Repman is a PHP repository manager. Main features: free and open source works as a proxy for packagist.org (speeds up

Repman 438 Jan 2, 2023
This script allows to bypass Oracle Cloud Infrastructure 'Out of host capacity' error immediately when additional OCI capacity will appear in your Home Region / Availability domain.

Resolving Oracle Cloud "Out of Capacity" issue and getting free VPS with 4 ARM cores / 24GB of memory Very neat and useful configuration was recently

Alexander Hitrov 323 Jan 6, 2023
Run Telegram PHP Bot on Windows Localhost without Host or VPN.

Run Telegram PHP Bot on Windows Localhost without Host or VPN.

iNeoTeam | آی نئو 4 May 30, 2022
Patches that prevent malicious Minecraft plugins from saturating host internet resources for DDoS.

Minecraft Host DoS Botnet Patches Patches that prevent malicious Minecraft plugins from saturating host internet resources for DDoS. In recent events,

Riley Nevins 4 Jul 16, 2022
A customizable decentralized micro-blog (Twitter) and cms (Wordpress) that only you host and moderate.

Consider this --> each Miter account is individually hosted on a user's personal and private third party server (host) - either purchased or free - as

Vije Miller 3 Jun 8, 2022
salah eddine bendyab 18 Aug 17, 2021