Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Last active May 1, 2024 18:46
Show Gist options
  • Save surferxo3/8b4eafe499f7fa52eacb6cc187d0a49a to your computer and use it in GitHub Desktop.
Save surferxo3/8b4eafe499f7fa52eacb6cc187d0a49a to your computer and use it in GitHub Desktop.
Gold and Silver Rates API to fetch updated rates with their respective currency and price.
<?php
/*#############################
* Developer: Mohammad Sharaf Ali
* Designation: Web Developer
* Version: 1.0
*/#############################
const RATES_API_URL = 'http://goldprice.org/NewCharts/gold/images/goldsilverpricesforcalculators.txt';
/*
* since the api returns text response so file_get_contents() will suffice
* else use curl get request
*/
$fileContent = file_get_contents(RATES_API_URL);
$silverFileContent = explode('!', $fileContent);
$goldFileContent = explode('@', $silverFileContent[1]);
$currAndRateCollection = array();
/*
* calculating gold and silver rates in different for each loops
* as the currencies return may vary across both
*/
#gold rates calculation
$currAndRates = explode(';', $goldFileContent[0]);
foreach ($currAndRates as $currAndRate) {
if (!empty($currAndRate)) {
$split = explode(',', $currAndRate);
$currAndRateCollection['gold_rates'][] = array('Currency' => str_replace('XAU_', '', $split[0]),
'Price' => $split[1]);
}
}
#silver rates calculation
$currAndRates = explode(';', $silverFileContent[2]);
foreach ($currAndRates as $currAndRate) {
if (!empty($currAndRate)) {
$split = explode(',', $currAndRate);
$currAndRateCollection['silver_rates'][] = array('Currency' => str_replace('XAG_', '', $split[0]),
'Price' => $split[1]);
}
}
echo json_encode($currAndRateCollection);
@novecento
Copy link

thanks.
do you have any others links to get prices?

@StevenTso
Copy link

thanks. do you have any others links to get prices?

Give this a try https://gist.github.com/StevenTso/b340fac0718252071e91017d7c53102e

@novecento
Copy link

Thanks a lot @StevenTso
if I can ask.. but if i would subscribe to "official" prices , like zurigo or london exchanges.. do you know a services ? obviously paying..

@anburocky3
Copy link

The website is now using the following link(s) to fetch the rates:

To test out the API, substitute it with different values.

How to get the values in g (grams), i need to get the gold price in India.

@othmanjabes
Copy link

@anburocky3
you can calculate it if you have price of ounce
search in google about the math
its easy.
((price / ounce) * rank gold) / 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment