Skip to content

Commit 0af6dbb

Browse files
Initial release v1.0.0
PHP SDK for MetalpriceAPI with support for all endpoints: fetchSymbols, fetchLive, fetchHistorical, hourly, ohlc, convert, timeframe, change, carat, usage. Includes setServer for runtime region switching.
0 parents  commit 0af6dbb

6 files changed

Lines changed: 501 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2025 MetalpriceAPI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# metalpriceapi
2+
3+
metalpriceapi is the official PHP wrapper for MetalpriceAPI.com. This allows you to quickly integrate our metal price API and foreign exchange rate API into your application. Check https://metalpriceapi.com documentation for more information.
4+
5+
## Installation
6+
7+
#### Composer
8+
9+
```
10+
$ composer require metalpriceapi/metalpriceapi-php
11+
```
12+
---
13+
## Usage
14+
15+
```php
16+
use MetalpriceAPI\Client;
17+
18+
$client = new Client('SET_YOUR_API_KEY_HERE');
19+
$result = $client->fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT']);
20+
```
21+
---
22+
## Server Regions
23+
24+
MetalpriceAPI provides two regional endpoints. Choose the one closest to your servers for optimal performance.
25+
26+
| Region | Base URL |
27+
|--------|----------|
28+
| United States (default) | `https://api.metalpriceapi.com/v1` |
29+
| Europe | `https://api-eu.metalpriceapi.com/v1` |
30+
31+
```php
32+
use MetalpriceAPI\Client;
33+
34+
// Default (US)
35+
$client = new Client('SET_YOUR_API_KEY_HERE');
36+
37+
// Europe
38+
$client = new Client('SET_YOUR_API_KEY_HERE', Client::BASE_URL_EU);
39+
```
40+
41+
---
42+
## setServer
43+
44+
You can switch server regions at runtime using the `setServer` method:
45+
46+
```php
47+
$client = new Client('SET_YOUR_API_KEY_HERE');
48+
49+
// Switch to EU server
50+
$client->setServer('eu');
51+
52+
// Switch back to US server
53+
$client->setServer('us');
54+
```
55+
56+
---
57+
## Documentation
58+
59+
#### fetchSymbols()
60+
```php
61+
$client->fetchSymbols();
62+
```
63+
64+
[Link](https://metalpriceapi.com/documentation#api_symbol)
65+
66+
---
67+
#### fetchLive($base, $currencies, $unit, $purity, $math)
68+
69+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
70+
- `$currencies` <[array]<[string]>> Optional. Pass in an array of currencies to return values for.
71+
- `$unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
72+
- `$purity` <[string]> Optional. Pass in a purity level for metal prices.
73+
- `$math` <[string]> Optional. Pass in a math expression to apply to the rates.
74+
75+
```php
76+
$client->fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz', null, null);
77+
```
78+
79+
[Link](https://metalpriceapi.com/documentation#api_realtime)
80+
81+
---
82+
#### fetchHistorical($date, $base, $currencies, $unit)
83+
84+
- `$date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
85+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
86+
- `$currencies` <[array]<[string]>> Optional. Pass in an array of currencies to return values for.
87+
- `$unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
88+
89+
```php
90+
$client->fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
91+
```
92+
93+
[Link](https://metalpriceapi.com/documentation#api_historical)
94+
95+
---
96+
#### hourly($base, $currency, $unit, $startDate, $endDate, $math, $dateType)
97+
98+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
99+
- `$currency` <[string]> Required. Specify currency you would like to get hourly rates for.
100+
- `$unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
101+
- `$startDate` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
102+
- `$endDate` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
103+
- `$math` <[string]> Optional. Pass in a math expression to apply to the rates.
104+
- `$dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
105+
106+
```php
107+
$client->hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03', null, null);
108+
```
109+
110+
[Link](https://metalpriceapi.com/documentation#api_hourly)
111+
112+
---
113+
#### ohlc($base, $currency, $date, $unit, $dateType)
114+
115+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
116+
- `$currency` <[string]> Required. Specify currency you would like to get OHLC for.
117+
- `$date` <[string]> Required. Specify date to get OHLC for specific date using format `YYYY-MM-DD`.
118+
- `$unit` <[string]> Optional. Pass in a unit, defaults to troy_oz.
119+
- `$dateType` <[string]> Optional. Pass in a date type, overrides date parameter if passed in.
120+
121+
```php
122+
$client->ohlc('USD', 'XAU', '2024-02-06', 'troy_oz', null);
123+
```
124+
125+
[Link](https://metalpriceapi.com/documentation#api_ohlc)
126+
127+
---
128+
#### convert($from, $to, $amount, $date, $unit)
129+
130+
- `$from` <[string]> Optional. Pass in a base currency, defaults to USD.
131+
- `$to` <[string]> Required. Specify currency you would like to convert to.
132+
- `$amount` <[number]> Required. The amount to convert.
133+
- `$date` <[string]> Optional. Specify date to use historical midpoint value for conversion with format `YYYY-MM-DD`. Otherwise, it will use live exchange rate date if value not passed in.
134+
- `$unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
135+
136+
```php
137+
$client->convert('USD', 'EUR', 100, '2024-02-05', null);
138+
```
139+
140+
[Link](https://metalpriceapi.com/documentation#api_convert)
141+
142+
---
143+
#### timeframe($startDate, $endDate, $base, $currencies, $unit)
144+
145+
- `$startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
146+
- `$endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
147+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
148+
- `$currencies` <[array]<[string]>> Optional. Pass in an array of currencies to return values for.
149+
- `$unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
150+
151+
```php
152+
$client->timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
153+
```
154+
155+
[Link](https://metalpriceapi.com/documentation#api_timeframe)
156+
157+
---
158+
#### change($startDate, $endDate, $base, $currencies, $dateType)
159+
160+
- `$startDate` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
161+
- `$endDate` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
162+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
163+
- `$currencies` <[array]<[string]>> Optional. Pass in an array of currencies to return values for.
164+
- `$dateType` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
165+
166+
```php
167+
$client->change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], null);
168+
```
169+
170+
[Link](https://metalpriceapi.com/documentation#api_change)
171+
172+
---
173+
#### carat($base, $currency, $date)
174+
175+
- `$base` <[string]> Optional. Pass in a base currency, defaults to USD.
176+
- `$currency` <[string]> Optional. Pass in a metal code to get carat prices for (defaults to XAU).
177+
- `$date` <[string]> Optional. Specify date to get Carat for specific date using format `YYYY-MM-DD`. If not specified, uses live rates.
178+
179+
```php
180+
$client->carat('USD', 'XAU', '2024-02-06');
181+
```
182+
183+
[Link](https://metalpriceapi.com/documentation#api_carat)
184+
185+
---
186+
#### usage()
187+
188+
```php
189+
$client->usage();
190+
```
191+
192+
[Link](https://metalpriceapi.com/documentation#api_usage)
193+
194+
---
195+
**[Official documentation](https://metalpriceapi.com/documentation)**
196+
197+
---
198+
## FAQ
199+
200+
- How do I get an API Key?
201+
202+
Free API Keys are available [here](https://metalpriceapi.com).
203+
204+
- I want more information
205+
206+
Checkout our FAQs [here](https://metalpriceapi.com/faq).
207+
208+
209+
## Support
210+
211+
For support, get in touch using [this form](https://metalpriceapi.com/contact).
212+
213+
214+
[array]: https://www.php.net/manual/en/language.types.array.php 'Array'
215+
[number]: https://www.php.net/manual/en/language.types.float.php 'Number'
216+
[string]: https://www.php.net/manual/en/language.types.string.php 'String'

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "metalpriceapi/metalpriceapi-php",
3+
"description": "Official PHP wrapper for MetalpriceAPI.com - Metal price API and foreign exchange rate API",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "https://metalpriceapi.com",
7+
"keywords": [
8+
"metal",
9+
"gold",
10+
"silver",
11+
"platinum",
12+
"palladium",
13+
"price",
14+
"api",
15+
"exchange",
16+
"rates",
17+
"forex"
18+
],
19+
"authors": [
20+
{
21+
"name": "MetalpriceAPI",
22+
"email": "contact@metalpriceapi.com",
23+
"homepage": "https://metalpriceapi.com"
24+
}
25+
],
26+
"support": {
27+
"issues": "https://github.com/metalpriceapi/metalpriceapi-php/issues",
28+
"source": "https://github.com/metalpriceapi/metalpriceapi-php"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "https://github.com/metalpriceapi/metalpriceapi-php.git"
33+
},
34+
"require": {
35+
"php": ">=7.4",
36+
"ext-curl": "*",
37+
"ext-json": "*"
38+
},
39+
"autoload": {
40+
"psr-4": {
41+
"MetalpriceAPI\\": "src/"
42+
}
43+
}
44+
}

example/index.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
// Or if not using Composer: require_once __DIR__ . '/../src/Client.php';
5+
6+
use MetalpriceAPI\Client;
7+
8+
$apiKey = 'REPLACE_ME';
9+
10+
// Default (US server)
11+
$client = new Client($apiKey);
12+
13+
// Or use the EU server for lower latency in Europe:
14+
// $client = new Client($apiKey, Client::BASE_URL_EU);
15+
16+
$result = $client->fetchSymbols();
17+
print_r($result);
18+
19+
$result = $client->fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz', null, null);
20+
print_r($result);
21+
22+
$result = $client->fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
23+
print_r($result);
24+
25+
$result = $client->hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03', null, null);
26+
print_r($result);
27+
28+
$result = $client->ohlc('USD', 'XAU', '2024-02-06', 'troy_oz', null);
29+
print_r($result);
30+
31+
$result = $client->convert('USD', 'EUR', 100, '2024-02-05', null);
32+
print_r($result);
33+
34+
$result = $client->timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz');
35+
print_r($result);
36+
37+
$result = $client->change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], null);
38+
print_r($result);
39+
40+
$result = $client->carat('USD', 'XAU', '2024-02-06');
41+
print_r($result);
42+
43+
$result = $client->usage();
44+
print_r($result);

0 commit comments

Comments
 (0)