Last active
May 1, 2024 18:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
@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
How to get the values in g (grams), i need to get the gold price in India.