|
| 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' |
0 commit comments