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);
@sgangas
Copy link

sgangas commented Feb 14, 2018

Hello,
I tested this above code with certain modifications, it works fine. Now how and where to get Gold Price API on daily basis to be used in php code or as json format.

thanks

@aershov24
Copy link

Hello,
I tested this above code with certain modifications, it works fine. Now how and where to get Gold Price API on daily basis to be used in php code or as json format.

thanks

Hi, look at www.goldapi.io. That is a Gold & Silver Prices Live & Historical REST JSON API. LBMA and COMEX are data sources. Please give a try. Full Disclosure: I'm a developer and ready to answer any questions regarding this API.

@surferxo3
Copy link
Author

Hello,
I tested this above code with certain modifications, it works fine. Now how and where to get Gold Price API on daily basis to be used in php code or as json format.

thanks

I have gone through ample of API's and they weren't free anyhow so I implemented it as a data scrapper. For daily rates you can schedule a Cron job (lets say every midnight) to trigger this script and store its results in DB and query from it.

@Tushark690
Copy link

Tushark690 commented Jun 25, 2020

Can you show some code or any refrence!?

@atifkhan-khan
Copy link

Any one share currency exchange Json format or API link...

@surferxo3
Copy link
Author

Any one share currency exchange Json format or API link...

https://exchangeratesapi.io/

@surferxo3
Copy link
Author

surferxo3 commented Aug 22, 2020

Can you show some code or any refrence!?

To fetch and store data from API: https://cmstoddler.com/curl-request-insert-record-mysql-database-php
To schedule a Cron Job: https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

@avatar-lavventura
Copy link

avatar-lavventura commented Oct 2, 2020

Does it get the rates as 15 minutes late?

@awais1206
Copy link

image
How can I get this kind of graph for gold price for a specific city, country

@awais1206
Copy link

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

@novecento
Copy link

do we have new link ?

@surferxo3
Copy link
Author

surferxo3 commented Feb 14, 2022

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

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

@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