Skip to content

Commit 05f893e

Browse files
committed
Used guzzle's service description to make requests to fixer.io api
0 parents  commit 05f893e

4 files changed

Lines changed: 350 additions & 0 deletions

File tree

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Fixer.io PHP Client
2+
3+
Provides an easy to use client for [fixer.io](https://fixer.io) exchange rates and currency conversion JSON API.
4+
5+
## Installation
6+
7+
This project can be installed using Composer:
8+
9+
``composer require ranium/fixerio-php-client``
10+
11+
## Usage
12+
13+
This package uses's [Guzzle's Service Description](https://github.com/guzzle/guzzle-services) to make HTTP Requests to the fixer.io API.
14+
15+
To use this package in your application, simply use the package and instantiate the client as follows
16+
17+
```php
18+
use Ranium\Fixerio\Client;
19+
20+
$accessKey = '12345678901234567890';
21+
$secure = true; // Optional, default is true (only paid plans of fixer.io supports SSL)
22+
$config = []; // Optional, guzzle command client config that you might want to pass
23+
24+
$fixerio = Client::create($accessKey, $secure, $config);
25+
```
26+
27+
Here's how you can access the fixer.io's endpoints:
28+
29+
*Note: `access_key` parameter is sent by default in all requests so no need to pass it in the argument while calling methods. However, you can include it should you want to override the access key used while instantiating the fixerio client.*
30+
31+
### [Latest rates endpoint](https://fixer.io/documentation#latestrates)
32+
33+
```php
34+
$fixerio->latest(
35+
[
36+
'base' => 'USD', // optional
37+
'symbols' => 'INR', // optional
38+
]
39+
);
40+
```
41+
42+
### [Historical rates endpoint](https://fixer.io/documentation#historicalrates)
43+
44+
```php
45+
$fixerio->historical(
46+
[
47+
'date' => '2019-01-01',
48+
'base' => 'USD', // optional
49+
'symbols' => 'INR', //optional
50+
]
51+
);
52+
```
53+
54+
### [Convert endpoint](https://fixer.io/documentation#convertcurrency)
55+
56+
```php
57+
$fixerio->convert(
58+
[
59+
'from' => 'USD',
60+
'to' => 'INR',
61+
'amount' => 50.75,
62+
'date' => '2019-01-01', //optional
63+
]
64+
);
65+
```
66+
67+
### [Time-Series data endpoint](https://fixer.io/documentation#timeseries)
68+
69+
```php
70+
$fixerio->timeseries(
71+
[
72+
'start_date' => '2019-01-01',
73+
'end_date' => '2019-01-05',
74+
'base' => 'USD', // optional
75+
'symbols' => 'INR', //optional
76+
]
77+
);
78+
```
79+
80+
### [Fluctuation data endpoint](https://fixer.io/documentation#timeseries)
81+
82+
```php
83+
$fixerio->fluctuation(
84+
[
85+
'start_date' => '2019-01-01',
86+
'end_date' => '2019-01-05',
87+
'base' => 'USD', // optional
88+
'symbols' => 'INR', //optional
89+
]
90+
);
91+
```
92+
93+
The response for all the above calls will be a JSON object. Please refer fixer.io's [documentation](https://fixer.io/documentation) for further details about various endpoints, request parameters and response objects.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ranium/fixerio-php-client",
3+
"description": "A PHP client for fixer.io foreign exchange rates and currency conversion API.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Abbas Ali",
9+
"email": "abbas@ranium.in"
10+
}
11+
],
12+
"require": {
13+
"guzzlehttp/guzzle": "~6.0",
14+
"guzzlehttp/guzzle-services": "*"
15+
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Ranium\\Fixerio\\": "src"
20+
}
21+
}
22+
}

service.json

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"name": "Fixerio",
3+
"apiVersion": "2019-02-04",
4+
"description": "A foreign exchanges rates and currency conversion JSON API.",
5+
"operations": {
6+
"latest": {
7+
"httpMethod": "GET",
8+
"uri": "/api/latest",
9+
"summary": "Gets real-time exchange rate data",
10+
"responseModel": "FixerioResponse",
11+
"parameters": {
12+
"access_key": {
13+
"location": "query",
14+
"type": "string",
15+
"required": true
16+
},
17+
"base": {
18+
"location": "query",
19+
"type": "string"
20+
},
21+
"symbols": {
22+
"location": "query",
23+
"type": "string"
24+
}
25+
}
26+
},
27+
"historical": {
28+
"httpMethod": "GET",
29+
"uri": "/api/{date}",
30+
"summary": "Gets historical exchange rate data",
31+
"responseModel": "FixerioResponse",
32+
"parameters": {
33+
"access_key": {
34+
"location": "query",
35+
"type": "string",
36+
"required": true
37+
},
38+
"date": {
39+
"location": "uri",
40+
"type": "string",
41+
"required": true
42+
},
43+
"base": {
44+
"location": "query",
45+
"type": "string"
46+
},
47+
"symbols": {
48+
"location": "query",
49+
"type": "string"
50+
}
51+
}
52+
},
53+
"convert": {
54+
"httpMethod": "GET",
55+
"uri": "/api/convert",
56+
"summary": "Convert any amount from one currency to another",
57+
"responseModel": "FixerioResponse",
58+
"parameters": {
59+
"access_key": {
60+
"location": "query",
61+
"type": "string",
62+
"required": true
63+
},
64+
"from": {
65+
"location": "query",
66+
"type": "string",
67+
"required": true
68+
},
69+
"to": {
70+
"location": "query",
71+
"type": "string",
72+
"required": true
73+
},
74+
"amount": {
75+
"location": "query",
76+
"type": "numeric",
77+
"required": true
78+
},
79+
"date": {
80+
"location": "query",
81+
"type": "string"
82+
}
83+
}
84+
},
85+
"timeseries": {
86+
"httpMethod": "GET",
87+
"uri": "/api/timeseries",
88+
"summary": "Get daily historical rates between two dates",
89+
"responseModel": "FixerioResponse",
90+
"parameters": {
91+
"access_key": {
92+
"location": "query",
93+
"type": "string",
94+
"required": true
95+
},
96+
"start_date": {
97+
"location": "query",
98+
"type": "string",
99+
"required": true
100+
},
101+
"end_date": {
102+
"location": "query",
103+
"type": "string",
104+
"required": true
105+
},
106+
"base": {
107+
"location": "query",
108+
"type": "string"
109+
},
110+
"symbols": {
111+
"location": "query",
112+
"type": "string"
113+
}
114+
}
115+
},
116+
"fluctuation": {
117+
"httpMethod": "GET",
118+
"uri": "/api/fluctuation",
119+
"summary": "Get currency fluctuation on a day-to-day basis",
120+
"responseModel": "FixerioResponse",
121+
"parameters": {
122+
"access_key": {
123+
"location": "query",
124+
"type": "string",
125+
"required": true
126+
},
127+
"start_date": {
128+
"location": "query",
129+
"type": "string",
130+
"required": true
131+
},
132+
"end_date": {
133+
"location": "query",
134+
"type": "string",
135+
"required": true
136+
},
137+
"base": {
138+
"location": "query",
139+
"type": "string"
140+
},
141+
"symbols": {
142+
"location": "query",
143+
"type": "string"
144+
}
145+
}
146+
}
147+
},
148+
"models": {
149+
"FixerioResponse": {
150+
"type": "object",
151+
"properties": {
152+
"success": {
153+
"type": "string",
154+
"required": true
155+
},
156+
"errors": {
157+
"type": "array",
158+
"items": {
159+
"type": "object",
160+
"properties": {
161+
"code": {
162+
"type": "integer",
163+
"description": "The error code."
164+
},
165+
"type": {
166+
"type": "string",
167+
"description": "The type of error."
168+
},
169+
"info": {
170+
"type": "string",
171+
"description": "The detailed message from the server."
172+
}
173+
}
174+
}
175+
}
176+
},
177+
"additionalProperties": {
178+
"location": "json"
179+
}
180+
}
181+
}
182+
}

src/Client.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Ranium\Fixerio;
3+
4+
use GuzzleHttp\Client as GuzzleHttpClient;
5+
use GuzzleHttp\Command\Guzzle\Description;
6+
use GuzzleHttp\Command\Guzzle\GuzzleClient;
7+
8+
/**
9+
* Fixerio HTTP Client class.
10+
*
11+
* This class extends GuzzleClient and uses Guzzle's Service Description
12+
* to make the http requests
13+
*
14+
* @author Abbas Ali <abbas@ranium.in>
15+
*/
16+
class Client extends GuzzleClient
17+
{
18+
/**
19+
* Method to instantiate the Fixerio web service client
20+
*
21+
* @param string $accessKey Fixer.io access key
22+
* @param boolean $secure Whether to send secure request (https)
23+
* @param array $config Extra config options to pass to http client
24+
*
25+
* @return Ranium\Fixerio\Client
26+
*/
27+
public static function create($accessKey, $secure = true, $config = [])
28+
{
29+
30+
$baseUrl = ($secure == true ? 'https' : 'http' ) . '://data.fixer.io/';
31+
32+
// Load the service description file.
33+
$serviceDescription = new Description(
34+
['baseUrl' => $baseUrl] +
35+
(array) json_decode(file_get_contents(__DIR__ . '/../service.json'), true)
36+
);
37+
38+
// Creates the client and sets the default request headers.
39+
$client = new GuzzleHttpClient(
40+
[
41+
'headers' => [
42+
'Content-Type' => 'application/json',
43+
'Accept' => 'application/json',
44+
]
45+
]
46+
);
47+
48+
// Put the access key as the default parameter for all commands/requests
49+
$config = $config + ['defaults' => ['access_key' => $accessKey]];
50+
51+
return new static($client, $serviceDescription, null, null, null, $config);
52+
}
53+
}

0 commit comments

Comments
 (0)