GithubHelp home page GithubHelp logo

tibahut / fixerio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fadion/fixerio

0.0 0.0 0.0 34 KB

PHP wrapper for Fixer.io, a service for foreign exchange rates

License: MIT License

PHP 100.00%

fixerio's Introduction

Thin wrapper for Fixer.io

A thin wrapper for Fixer.io, a service for foreign exchange rates and currency conversion. It provides a few methods to easily construct the url, makes the api call and gives back the response.

Installation

  • Add the package to your composer.json file and run composer update:
{
    "require": {
        "tibahut/fixerio": "~1.0"
    }
}

Laravel users can use the Facade for even easier access.

  • Add Tibahut\Fixerio\ExchangeServiceProvider::class to your config/app.php file, inside the providers array.
  • Add a new alias: 'Exchange' => Tibahut\Fixerio\Facades\Exchange::class to your config/app.php file, inside the aliases array.

Usage

Let's get the rates of EUR and GBP with USD as the base currency:

use Tibahut\Fixerio\Exchange;
use Tibahut\Fixerio\Currency;

$exchange = new Exchange();
$exchange->key("YOUR_ACCESS_KEY");
$exchange->base(Currency::USD);
$exchange->symbols(Currency::EUR, Currency::GBP);

$rates = $exchange->get();

By default, the base currency is EUR, so if that's your base, there's no need to set it. The symbols can be omitted too, as Fixer will return all the supported currencies.

A simplified example without the base and currency:

$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->get();

The historical option will return currency rates for every day since the date you've specified. The base currency and symbols can be omitted here to, but let's see a full example:

$exchange = new Exchange();
$exchange->key("YOUR_ACCESS_KEY");
$exchange->historical('2012-12-12');
$exchange->base(Currency::AUD);
$exchange->symbols(Currency::USD, Currency::EUR, Currency::GBP);

$rates = $exchange->get();

Finally, you may have noticed the use of the Currency class with currencies as constants. It's just a convenience to prevent errors from typos, but they're completely optional.

This:

$exchange->base(Currency::AUD);
$exchange->symbols(Currency::USD, Currency::EUR, Currency::GBP);

is equivalent to:

$exchange->base('AUD');
$exchange->symbols('USD', 'EUR', 'GBP');

Use whatever method fills your needs.

Response

The response is a simple array with currencies as keys and ratios as values. For a request like the following:

$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->get();

the response will be an array:

array('GBP' => 0.7009, 'USD' => 1.0666)

which you can access with the keys as strings or using the currency constants:

print $rates['EUR'];
print $rates[Currency::GBP];

There is an option to handle the response as an object:

$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->getAsObject();

print $rates->USD;
print $rates->GBP;

The last option is to return the response as a Result class. This allows access to the full set of properties returned from the feed.

$result = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->getResult();

$date = $result->getDate(); // The date the data is from
$rates = $result->getRates(); // Array of rates as above
$usd = $result->getRate(Currency::USD); // Will return null if there was no value

Error Handling

To handle errors, the package provides 2 exceptions. ConnectionException when http requests go wrong and ResponseException when the returned response from the api is not as expected. An example with exception handling:

use Tibahut\Fixerio\Exchange;
use Tibahut\Fixerio\Exceptions\ConnectionException;
use Tibahut\Fixerio\Exceptions\ResponseException;

try {
    $exchange = new Exchange();
    $exchange->key("YOUR_ACCESS_KEY");
    $rates = $exchange->get();
}
catch (ConnectionException $e) {
    // handle
}
catch (ResponseException $e) {
    // handle
}

Laravel Usage

Nothing changes for Laravel apart from the Facade. It's just a convenience for a tad shorter way of using the package:

use Exchange;
use Tibahut\Fixerio\Currency;

$rates = Exchange::base(Currency::USD)->get();

To use this Facade, you should set your access key in your config/services.php file:

'fixer'=>[
    'key'=>env("FIXER_ACCESS_KEY"),
]

fixerio's People

Contributors

fadion avatar tibahut avatar remyvhw avatar djmarland avatar wstaples avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.