-
-
Save surferxo3/8b4eafe499f7fa52eacb6cc187d0a49a to your computer and use it in GitHub Desktop.
<?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); |
The website is now using the following link(s) to fetch the rates:
To test out the API, substitute it with different values.
thanks.
do you have any others links to get prices?
thanks. do you have any others links to get prices?
Give this a try https://gist.github.com/StevenTso/b340fac0718252071e91017d7c53102e
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..
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.
@anburocky3
you can calculate it if you have price of ounce
search in google about the math
its easy.
((price / ounce) * rank gold) / 24
@anburocky3 you can calculate it if you have price of ounce search in google about the math its easy. ((price / ounce) * rank gold) / 24
can you explain with example
@anburocky3 you can calculate it if you have price of ounce search in google about the math its easy. ((price / ounce) * rank gold) / 24
can you explain with example
if the ounce price now is 2500 usd
and in india you buy or sell gold rank 24
===> how many grams is the ounce => ounce = 31.1034768 grams
((price / ounce how many grams) * rank gold) / 24
((2500/31.1034768)*24)/24
=80.3$
the price of one grams of gold 24k => 80.3$
change only the rank gold & price
((price / ounce how many grams) * rank gold) / 24
((2500/31.1034768)*24)/24
((2500/31.1034768)*22)/24
((2500/31.1034768)*21)/24
Release Notes for Version 2.0
Changes from Version 1.0:
API Endpoint Update:
- The script now uses a new API endpoint that returns data in JSON format instead of plain text. This change improves data handling and flexibility
cURL Integration:
- Replaced
file_get_contents
with cURL to fetch data. This update enhances error handling and performance.
Enhanced Price Calculations:
- Added calculations for gold and silver prices in grams (G), kilograms (KG), and tolas in addition to ounces (OZ). This provides more detailed pricing information.
- Added calculations for gold prices in various carats: 24K, 22K, 21K, and 18K
Supported Currencies List:
- Included a comprehensive list of supported currencies for better reference. Users can now easily see which currencies are available for fetching price data.
Sample Output:
{
"USD": {
"gold_rates": {
"Price_OZ": 2505.30,
"Price_G": 80.55,
"Price_KG": 80547.21,
"Price_Tola": 939.18,
"Price_24K": 2505.30,
"Price_22K": 2296.61,
"Price_21K": 2192.14,
"Price_18K": 1878.98
},
"silver_rates": {
"Price_OZ": 28.50,
"Price_G": 0.92,
"Price_KG": 916.27,
"Price_Tola": 10.68
}
}
}
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?
@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?
do we have new link ?