rochi88/laravel-open-exchange-rates

用于与Open Exchange Rates API交互的包。

dev-main 2022-02-12 06:02 UTC

This package is auto-updated.

Last update: 2024-09-16 15:54:29 UTC


README

此包提供了一种简单便捷的接口来使用Open Exchange Rates服务。目前支持免费端点。

通过Composer安装

将以下内容添加到您的composer.json文件中的require对象

  "repositories": [
        {
            "type": "git",
            "url": "https://github.com/rochi88/laravel-open-exchange-rates"
        }
    ],
    "require": {
        "rochi88/laravel-open-exchange-rates": "dev-main",
    },

之后,运行composer install来安装包。

最后,发布配置文件。

php artisan vendor:publish --provider="Rochi88\LaravelOpenExchangeRates\LOERServiceProvider"

使用示例

config/loer.php

return [
    'app_id' => '*****************************', // your own api key
    'default_base_currency' => 'USD'
];

在控制器(或服务)方法中

use Rochi88\LaravelOpenExchangeRates\Client;

class SomeController extends Controller
{
    private $client;
    
    public function __construct(Client $client)
    {
        $this->client = $client;
    }
    
    public function someAction()
    {
        $coefficient = $this->client->latest('USD,RUB,AWG');
        
        $historical_coefficent $this->client->historical('2011-03-05', 'USD,RUB,AWG');
        
        // e.t.c...
        
        // Change in base currencies (not allowed for free account) and requests for the coefficients of all currencies relative to.
        $coefficient = $this->client->currency('RUB')->latest();
    }
}