PHP code to download the XML file at set interval.

517 views
Skip to first unread message

DGCSC XML Price Service

unread,
Jan 18, 2012, 12:24:51 PM1/18/12
to dgcsc-xml-...@googlegroups.com
If anyone has PHP code that downloads the XML rates at regular intervals, and is willing to share, please post it here. There are new users that are unable to work this out for themselves.

Thanks,

Sidd. 

Mike Christopher

unread,
Apr 16, 2012, 2:59:37 PM4/16/12
to dgcsc-xml-...@googlegroups.com
I use cron to call a script. The cron runs every 15 mins mon-fri

here's the script

<?

// check if COMEX hours from CST based server

$time = date("Gi"); if ( $time >= '619' && $time <= '1230'){ getPrices(); }

function getPrices(){
require_once "config.php"; // change to yours

$url = 'https://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=GoldBE';
$xml = new SimpleXMLElement($url,null,true);
$gold = $xml->xpath("/GoldPrice/USD");

$url = 'https://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=SilverBE';
$xml = new SimpleXMLElement($url,null,true);
$silver = $xml->xpath("/SilverPrice/USD");
$date = $xml->attributes()->date;

$goldPriceGram = $gold[0];
$silverPriceGram = $silver[0];

$query = "UPDATE tbl SET date='$date', gold='$goldPriceGram', silver='$silverPriceGram' WHERE id = '1'";
$result = mysql_query($query) or die(mysql_error());
?>

I then query the database for the prices and date to use.

Data powers gold jewelry calculator and live metal price widgets at http://www.buysellgoldandsilver.com/free-gold-silver-widgets.php


Message has been deleted

Maeve Lander

unread,
Jun 11, 2020, 1:50:47 AM6/11/20
to DGCSC XML Developers
I also use a cron to call a script every 15 mins Mon-Fri.   Script shared below.

It uses file_get_contents to read the feed data, then file_put_contents to write that data to a file on my server - gold.xml, silver.xml etc   If the file does not exist, it would be created. Otherwise, the existing file is overwritten.

<?php

echo getxml();

function getxml(){
// Gold
$remote_gold_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=GoldBE");
$local_gold_path = 'gold.xml';
file_put_contents($local_gold_path, $remote_gold_contents);
$remote_gold_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=GoldBOE");
$local_gold_path = 'goldounces.xml';
file_put_contents($local_gold_path, $remote_gold_contents);

// Silver
$remote_silver_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=SilverBE");
$local_silver_path = 'silver.xml';
file_put_contents($local_silver_path, $remote_silver_contents);
$remote_silver_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=SilverBOE");
$local_silver_path = 'silverounces.xml';
file_put_contents($local_silver_path, $remote_silver_contents);

// Platinum
$remote_platinum_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=PlatinumBE");
$local_platinum_path = 'platinum.xml';
file_put_contents($local_platinum_path, $remote_platinum_contents);
$remote_platinum_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=PlatinumBOE");
$local_platinum_path = 'platinumounces.xml';
file_put_contents($local_platinum_path, $remote_platinum_contents);

// Palladium
$remote_palladium_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=PalladiumBE");
$local_palladium_path = 'palladium.xml';
file_put_contents($local_palladium_path, $remote_palladium_contents);
$remote_palladium_contents = file_get_contents("http://xml.dgcsc.org/xml.cfm?password=YOUR_PASSWORD&action=PalladiumBOE");
$local_palladium_path = 'palladiumounces.xml';
file_put_contents($local_palladium_path, $remote_palladium_contents);

}
?>


Data powers a WordPress plugin: https://wordpress.org/plugins/gold-price

Reply all
Reply to author
Forward
0 new messages