Last active
March 17, 2025 11:50
-
-
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 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: 2.0 | |
* | |
* Changes from Version 1.0: | |
* - Updated to use a new API endpoint that returns JSON data instead of plain text | |
* - Replaced file_get_contents with cURL for improved error handling and performance | |
* - Added calculations for gold and silver prices in grams (G), kilograms (KG), and tolas, in addition to ounces (OZ) | |
* - Added calculations for gold prices at 24K, 22K, 21K, and 18K purity levels | |
* - Included a list of supported currencies for better reference | |
* - Formatted final JSON output to 2 decimal places | |
* - Grouped data by currency for easier parsing | |
###############################*/ | |
/** | |
* API URL to fetch gold and silver prices | |
* | |
* By default, this URL fetches prices in USD | |
* Add more currencies by appending them as comma-separated values, e.g., 'PKR,INR,...' | |
*/ | |
const RATES_API_URL = 'https://data-asg.goldprice.org/dbXRates/USD'; | |
/** | |
* List of supported currencies: | |
* USD, AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, | |
* BHD, BIF, BMD, BND, BOB, BRL, BSD, BTN, BWP, BYN, BZD, CAD, CDF, CHF, CLP, | |
* CNY, COP, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, | |
* FJD, FKP, GBP, GEL, GGP, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, | |
* HUF, IDR, ILS, IMP, INR, IQD, IRR, ISK, JEP, JMD, JOD, JPY, KES, KGS, KHR, | |
* KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, | |
* MKD, MMK, MNT, MOP, MRU, MUR, MVR, MWK, MXN, MYR, MZN, NAD, NGN, NIO, NOK, | |
* NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF, | |
* SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, STD, SVC, SYP, SZL, THB, | |
* TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, UYU, UZS, VEF, VND, | |
* VUV, WST, XAF, XAG, XAU, XCD, XDR, XOF, XPD, XPF, XPT, YER, ZAR, ZMW | |
*/ | |
/** | |
* Conversion constants | |
* | |
* - OZ_TO_GRAM: Conversion factor from ounces to grams | |
* - GRAM_TO_KG: Conversion factor from grams to kilograms | |
* - TOLA_TO_GRAM: Conversion factor from tolas to grams | |
*/ | |
const OZ_TO_GRAM = 31.1035; | |
const GRAM_TO_KG = 1000; | |
const TOLA_TO_GRAM = 11.66; | |
// Gold purity factors for different carats | |
const PURITY_24K = 1.00; // 100% pure gold | |
const PURITY_22K = 0.9167; // 91.67% pure gold | |
const PURITY_21K = 0.8750; // 87.5% pure gold | |
const PURITY_18K = 0.7500; // 75% pure gold | |
// Initialize a cURL session to fetch data from the API | |
$ch = curl_init(RATES_API_URL); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the response as a string | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // disable SSL verification (useful for local testing) | |
$response = curl_exec($ch); | |
// Handle any cURL errors | |
if (curl_errno($ch)) { | |
echo 'cURL error: ' . curl_error($ch); | |
exit(); | |
} | |
curl_close($ch); // close the cURL session | |
// Decode the JSON response into a PHP array | |
$apiResponse = json_decode($response, true); | |
// Initialize an array to store calculated rates for gold and silver | |
$currAndRateCollection = []; | |
/** | |
* Calculate gold and silver prices in OZ, grams, kilograms, tolas, and various carats | |
*/ | |
foreach ($apiResponse['items'] as $item) { | |
$currency = $item['curr']; | |
$goldPriceOZ = $item['xauPrice']; // gold price per ounce | |
$silverPriceOZ = $item['xagPrice']; // silver price per ounce | |
// Convert prices to grams, kilograms, and tolas | |
$goldPriceG = $goldPriceOZ / OZ_TO_GRAM; // gold price per gram | |
$goldPriceKG = $goldPriceG * GRAM_TO_KG; // gold price per kilogram | |
$goldPriceTola = $goldPriceG * TOLA_TO_GRAM; // gold price per tola | |
// Calculate gold prices for different carats | |
$goldPrice24K = $goldPriceOZ * PURITY_24K; | |
$goldPrice22K = $goldPriceOZ * PURITY_22K; | |
$goldPrice21K = $goldPriceOZ * PURITY_21K; | |
$goldPrice18K = $goldPriceOZ * PURITY_18K; | |
$silverPriceG = $silverPriceOZ / OZ_TO_GRAM; | |
$silverPriceKG = $silverPriceG * GRAM_TO_KG; | |
$silverPriceTola = $silverPriceG * TOLA_TO_GRAM; | |
// Store gold prices grouped by currency | |
$currAndRateCollection[$currency]['gold_rates'] = [ | |
'Price_OZ' => round($goldPriceOZ, 2), | |
'Price_G' => round($goldPriceG, 2), | |
'Price_KG' => round($goldPriceKG, 2), | |
'Price_Tola' => round($goldPriceTola, 2), | |
'Price_24K' => round($goldPrice24K, 2), | |
'Price_22K' => round($goldPrice22K, 2), | |
'Price_21K' => round($goldPrice21K, 2), | |
'Price_18K' => round($goldPrice18K, 2) | |
]; | |
// Store silver prices grouped by currency | |
$currAndRateCollection[$currency]['silver_rates'] = [ | |
'Price_OZ' => round($silverPriceOZ, 2), | |
'Price_G' => round($silverPriceG, 2), | |
'Price_KG' => round($silverPriceKG, 2), | |
'Price_Tola' => round($silverPriceTola, 2) | |
]; | |
} | |
// Output the final JSON | |
echo json_encode($currAndRateCollection, JSON_PRETTY_PRINT); |
@wadid, you can use the below API for Platinum and Palladium rates
API Endpoint:
https://www.bullionbypost.co.uk/ajax/update-header-metal-prices/83cb35defe024/
Sample Output:
[
{
"last_updated": "11:08 30/09/24",
"platinum": {
"ounces": {
"usd": "990.50000000",
"gbp": "739.58200000",
"eur": "885.39500000"
},
"grams": {
"usd": "31.84531447622601470714039274",
"gbp": "23.77811344871901909049601812",
"eur": "28.46611025813037081436503587"
},
"kilograms": {
"usd": "31845.31447622601470714039274",
"gbp": "23778.11344871901909049601812",
"eur": "28466.11025813037081436503587"
}
},
"palladium": {
"ounces": {
"usd": "1012.62000000",
"gbp": "756.09800000",
"eur": "905.16800000"
},
"grams": {
"usd": "32.55648899032406563628925240",
"gbp": "24.30911517904647881679902743",
"eur": "29.10182697003185187322852601"
},
"kilograms": {
"usd": "32556.48899032406563628925240",
"gbp": "24309.11517904647881679902743",
"eur": "29101.82697003185187322852601"
}
}
}
]
@surferxo3 cool thx! there are no rate limits or anything on these free apis?
@surferxo3 I think we have to get price_update_token to use on URL.
In inner source of page I see:
var price_update_token = "e103de5a616e4";
WILL THE ABOVE SHARED CODE FETCH GOLD PRICE FOR ALL CURRENCIES FOR DIFFERENT UNITS AND MEASUREMENT, LIKE 10GM GOLD PRICE IN INDIA, 20 gm gold price in Dubai, etc or it will fetch only in one currency, unit for a specific country. Please answer my query
Have you found any solution?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice script! Is there any information about the api? Is there rate limiting or similar?
also do you know how to also get palladium and platinum?