Skip to content

Commit 8f6720e

Browse files
author
1Forge
committed
Added API key support
1 parent 457c80a commit 8f6720e

File tree

5 files changed

+120
-166
lines changed

5 files changed

+120
-166
lines changed

README.md

Lines changed: 32 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ ForexQuotes is a PHP Library for fetching realtime forex quotes
1010
- [List of Symbols available](#get-the-list-of-available-symbols)
1111
- [Get quotes for specific symbols](#get-quotes-for-specified-symbols)
1212
- [Convert from one currency to another](#convert-from-one-currency-to-another)
13-
- [Other implementations](#other-implementations)
14-
- [API Call: Get all quotes](#get-all-quotes)
15-
- [API Call: Get specific quotes](#get-specific-quotes)
16-
- [API Call: Get the symbol list](#get-the-symbol-list)
1713
- [Support / Contact](#support-and-contact)
1814
- [License / Terms](#license-and-terms)
1915

@@ -28,29 +24,39 @@ composer require oneforge/forexquotes
2824
Or in your composer.json
2925
```javascript
3026
"require": {
31-
"oneforge/forexquotes": "1.0.5"
27+
"oneforge/forexquotes": "2.0.0"
3228
},
3329
```
3430
## Usage
3531

32+
### Instantiate the client
33+
```php
34+
//You can get an API key for free at 1forge.com
35+
$client = new ForexDataClient('YOUR_API_KEY');
36+
```
37+
3638
### Get the list of available symbols:
3739

3840
```php
3941
<?php
4042

4143
use OneForge\ForexQuotes\ForexRequest;
4244

45+
$client = new ForexDataClient('YOUR_API_KEY');
46+
4347
/*
4448
Returns an array of symbols, eg: ['EURUSD', 'GBPJPY']
4549
*/
46-
ForexRequest::getSymbols();
50+
$client->getSymbols();
4751
```
4852
### Get quotes for specified symbols:
4953
```php
5054
<?php
5155

5256
use OneForge\ForexQuotes\ForexRequest;
5357

58+
$client = new ForexDataClient('YOUR_API_KEY');
59+
5460
/*
5561
Returns an array of quotes, eg:
5662
[
@@ -66,7 +72,7 @@ Returns an array of quotes, eg:
6672
],
6773
]
6874
*/
69-
ForexRequest::getQuotes([
75+
$client->getQuotes([
7076
'AUDUSD',
7177
'GBPJPY'
7278
]);
@@ -81,14 +87,16 @@ ForexRequest::getQuotes([
8187

8288
use OneForge\ForexQuotes\ForexRequest;
8389

90+
$client = new ForexDataClient('YOUR_API_KEY');
91+
8492
/*
8593

8694
[value] => 111.961
8795
[text] => 100 EUR is worth 111.961 USD
8896
[timestamp] => 1497187505
8997

9098
*/
91-
ForexRequest::convert('USD', 'EUR', 100);
99+
$client->convert('USD', 'EUR', 100);
92100
```
93101

94102

@@ -99,101 +107,38 @@ ForexRequest::convert('USD', 'EUR', 100);
99107

100108
use OneForge\ForexQuotes\ForexRequest;
101109

110+
$client = new ForexDataClient('YOUR_API_KEY');
111+
102112
/*
103113
Returns an boolean
104114
*/
105115

106-
if (ForexRequest::marketIsOpen())
116+
if ($client->marketIsOpen())
107117
{
108118
echo "Market is open";
109119
}
110120
```
111121

122+
### Check your usage / quota limit:
123+
```php
124+
<?php
112125

113-
## Other implementations
114-
You can also implement the api directly in any other way you wish. Full documentation is maintained here: <a href="https://1forge.com/forex-data-api">https://1forge.com/forex-data-api</a>
115-
116-
### Get specific quotes
117-
#### Request
118-
```
119-
GET https://forex.1forge.com/1.0.1/quotes?pairs=EURUSD,GBPJPY,AUDUSD
120-
```
121-
122-
#### Response
123-
```javascript
124-
[
125-
{
126-
symbol: "AUDUSD",
127-
timestamp: 1496096387,
128-
price: 0.74392,
129-
},
130-
{
131-
symbol: "EURUSD",
132-
timestamp: 1496096387,
133-
price: 1.11383,
134-
},
135-
{
136-
symbol: "GBPJPY",
137-
timestamp: 1496096387,
138-
price: 142.657,
139-
}
140-
]
141-
```
142-
143-
144-
### Get the symbol list
145-
#### Request
146-
```
147-
GET https://forex.1forge.com/1.0.1/symbols
148-
```
149-
150-
#### Response
151-
```javascript
152-
[
153-
"AUDJPY",
154-
"AUDUSD",
155-
"CHFJPY",
156-
"EURAUD",
157-
"EURCAD",
158-
"EURCHF",
159-
"EURGBP",
160-
"EURJPY",
161-
"etc..."
162-
]
163-
```
164-
165-
166-
### Convert from one currency to another
167-
#### Request
168-
```
169-
GET https://forex.1forge.com/1.0.1/convert?from=USD&to=EUR&quantity=100
170-
```
171-
172-
#### Response
173-
```javascript
126+
use OneForge\ForexQuotes\ForexRequest;
174127

175-
{
176-
value: 89.3164183,
177-
text: "100 USD is worth 89.3164183 EUR",
178-
timestamp: 1497186516
179-
}
128+
$client = new ForexDataClient('YOUR_API_KEY');
180129

181-
```
130+
/*
131+
[quota_used] => 53232,
132+
[quota_limit] => 100000,
133+
[quota_remaining] => 46768,
134+
[hours_until_reset] => 11
135+
136+
*/
182137

183-
### Get the market status
184-
#### Request
185-
```
186-
GET https://forex.1forge.com/1.0.1/market_status
138+
$client->quota();
187139
```
188140

189-
#### Response
190-
```javascript
191141

192-
{
193-
market_is_open: true
194-
}
195-
196-
```
197142

198143
## Support and Contact
199144
Please contact me at contact@1forge.com if you have any questions or requests.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "oneforge/forexquotes",
33
"type": "library",
4-
"version": "1.0.6",
4+
"version": "2.0.0",
55
"description": "Library to fetch and parse realtime Forex quotes and convert currencies",
66
"keywords": [
77
"forex",

example.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
require_once __DIR__ . '/vendor/autoload.php';
1111

12-
$quotes = ForexRequest::getQuotes(['AUDUSD',
13-
'GBPJPY']);
14-
$symbols = ForexRequest::getSymbols();
15-
$conversion = ForexRequest::convert('EUR', 'USD', 100);
16-
$market_is_open = ForexRequest::marketIsOpen();
12+
$client = new ForexDataClient('YOUR_API_KEY');
13+
$quotes = $client->getQuotes(['AUDUSD',
14+
'GBPJPY']);
15+
$symbols = $client->getSymbols();
16+
$conversion = $client->convert('EUR', 'USD', 100);
17+
$market_is_open = $client->marketIsOpen();
1718

1819
print_r($quotes);
1920
print_r($symbols);

lib/ForexDataClient.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This library is provided without warranty under the MIT license.
5+
* Created by 1Forge - http://1forge.com
6+
*/
7+
8+
namespace OneForge\ForexQuotes;
9+
10+
use GuzzleHttp\Client;
11+
12+
class ForexDataClient
13+
{
14+
private $api_key;
15+
16+
public function __construct($api_key)
17+
{
18+
$this->api_key = $api_key;
19+
20+
$this->client = new Client([// Base URI is used with relative requests
21+
'base_uri' => 'http://forex.1forge.com/1.0.2/',
22+
// You can set any number of default request options.
23+
'timeout' => 2.0,
24+
'headers' => ['Content-Type' => 'application/json']]);
25+
}
26+
27+
public function fetch($uri)
28+
{
29+
return $this->client->get($uri . '&api_key=' . $this->api_key)->getBody();
30+
}
31+
32+
public function getSymbols()
33+
{
34+
$body = $this->fetch('symbols?cache=false');
35+
36+
return json_decode($body, true);
37+
}
38+
39+
public function getQuotes(array $symbols)
40+
{
41+
$pairs = implode(",", $symbols);
42+
$body = $this->fetch('quotes?pairs=' . $pairs);
43+
44+
$quotes = json_decode($body);
45+
46+
$quotes_array = [];
47+
48+
foreach ($quotes as $quote)
49+
{
50+
$quotes_array[] = ['symbol' => $quote->symbol,
51+
'price' => $quote->price,
52+
'timestamp' => $quote->timestamp];
53+
}
54+
55+
return $quotes_array;
56+
}
57+
58+
public function marketIsOpen()
59+
{
60+
$body = $this->fetch('market_status?cache=false');
61+
62+
$body = json_decode($body);
63+
64+
if (property_exists($body, 'market_is_open'))
65+
{
66+
return (bool)$body->market_is_open;
67+
}
68+
69+
return false;
70+
}
71+
72+
public function convert($from, $to, $quantity)
73+
{
74+
$body = $this->fetch('convert?from=' . $from . '&to=' . $to . '&quantity=' . $quantity);
75+
76+
return json_decode($body, true);
77+
}
78+
79+
}
80+
81+

lib/ForexRequest.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)