-
-
Save aknik/02adb53f64be43a28b7ac57131503372 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 hidden or 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment