Link to home
Start Free TrialLog in
Avatar of RadioFreq
RadioFreq

asked on

How to stream live prices of gold on a website?

I would like to be able to put streaming or updated data on my website for the price of gold.
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

http://goldprice.org/gold-price-data.html

You need to purchase the feed at a cost defined by the feed provider.
Some currency converters have the spot price of gold.  It is usually referred to as XAU.  Here is a currency converter I have used as a teaching example.  You might be able to extract from and adapt it to your needs.  The quality and timeliness of your data source might or might not matter very much to you - that would depend on how your clients expect to use the information on the screen.  There are many sources that you might "scrape" or access via API.

http://www.goldprice.org/spot-gold.html
http://www.kitco.com/charts/livegold.html
http://www.monex.com/liveprices
http://widgets.yahoo.com/widgets/gold-spot

HTH, ~Ray
<?php // RAY_currency_converter.php
error_reporting(E_ALL);

// PHP 5.1+  SEE http://us3.php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

class CurrencyConverter
{
    protected $error, $norml, $terse, $verbose, $array, $rates;
    protected static $currs;

    public function __construct()
    {
        // READ THE EXCHANGE RATES
        $url = 'http://www.99currencies.com/currency/curr.php';
	$str = file_get_contents($url);
	$str = str_replace(':CUR', '', $str);
	$this->rates = json_decode($str, TRUE);

        // DATA ADAPTED FROM XE.COM - SEE ALSO: http://en.wikipedia.org/wiki/ISO_4217
        $this->currs["AED"] = array("N" => "United Arab Emirates",                                 "C" => "Dirhams");
        $this->currs["AFN"] = array("N" => "Afghanistan",                                          "C" => "Afghanis");
        $this->currs["ALL"] = array("N" => "Albania",                                              "C" => "Leke");
        $this->currs["AMD"] = array("N" => "Armenia",                                              "C" => "Drams");
        $this->currs["ANG"] = array("N" => "Netherlands Antilles",                                 "C" => "Guilders (also called Florins)");
        $this->currs["AOA"] = array("N" => "Angola",                                               "C" => "Kwanza");
        $this->currs["ARS"] = array("N" => "Argentina",                                            "C" => "Pesos");
        $this->currs["AUD"] = array("N" => "Australia",                                            "C" => "Dollars");
        $this->currs["AWG"] = array("N" => "Aruba",                                                "C" => "Guilders (also called Florins)");
        $this->currs["AZN"] = array("N" => "Azerbaijan",                                           "C" => "New Manats");
        $this->currs["BAM"] = array("N" => "Bosnia and Herzegovina",                               "C" => "Convertible Marka");
        $this->currs["BBD"] = array("N" => "Barbados",                                             "C" => "Dollars");
        $this->currs["BDT"] = array("N" => "Bangladesh",                                           "C" => "Taka");
        $this->currs["BGN"] = array("N" => "Bulgaria",                                             "C" => "Leva");
        $this->currs["BHD"] = array("N" => "Bahrain",                                              "C" => "Dinars");
        $this->currs["BIF"] = array("N" => "Burundi",                                              "C" => "Francs");
        $this->currs["BMD"] = array("N" => "Bermuda",                                              "C" => "Dollars");
        $this->currs["BND"] = array("N" => "Brunei Darussalam",                                    "C" => "Dollars");
        $this->currs["BOB"] = array("N" => "Bolivia",                                              "C" => "Bolivianos");
        $this->currs["BRL"] = array("N" => "Brazil",                                               "C" => "Brazil Real");
        $this->currs["BSD"] = array("N" => "Bahamas",                                              "C" => "Dollars");
        $this->currs["BTN"] = array("N" => "Bhutan",                                               "C" => "Ngultrum");
        $this->currs["BWP"] = array("N" => "Botswana",                                             "C" => "Pulas");
        $this->currs["BYR"] = array("N" => "Belarus",                                              "C" => "Rubles");
        $this->currs["BZD"] = array("N" => "Belize",                                               "C" => "Dollars");
        $this->currs["CAD"] = array("N" => "Canada",                                               "C" => "Dollars");
        $this->currs["CDF"] = array("N" => "Congo/Kinshasa",                                       "C" => "Congolese Francs");
        $this->currs["CHF"] = array("N" => "Switzerland",                                          "C" => "Francs");
        $this->currs["CLP"] = array("N" => "Chile",                                                "C" => "Pesos");
        $this->currs["CNY"] = array("N" => "China",                                                "C" => "Yuan Renminbi");
        $this->currs["COP"] = array("N" => "Colombia",                                             "C" => "Pesos");
        $this->currs["CRC"] = array("N" => "Costa Rica",                                           "C" => "Colones");
        $this->currs["CUP"] = array("N" => "Cuba",                                                 "C" => "Pesos");
        $this->currs["CVE"] = array("N" => "Cape Verde",                                           "C" => "Escudos");
        $this->currs["CYP"] = array("N" => "Cyprus",                                               "C" => "Pounds (expires 2008-Jan-31)");
        $this->currs["CZK"] = array("N" => "Czech Republic",                                       "C" => "Koruny");
        $this->currs["DJF"] = array("N" => "Djibouti",                                             "C" => "Francs");
        $this->currs["DKK"] = array("N" => "Denmark",                                              "C" => "Kroner");
        $this->currs["DOP"] = array("N" => "Dominican Republic",                                   "C" => "Pesos");
        $this->currs["DZD"] = array("N" => "Algeria",                                              "C" => "Algeria Dinars");
        $this->currs["EEK"] = array("N" => "Estonia",                                              "C" => "Krooni");
        $this->currs["EGP"] = array("N" => "Egypt",                                                "C" => "Pounds");
        $this->currs["ERN"] = array("N" => "Eritrea",                                              "C" => "Nakfa");
        $this->currs["ETB"] = array("N" => "Ethiopia",                                             "C" => "Birr");
        $this->currs["EUR"] = array("N" => "Euro Member Countries",                                "C" => "Euro");
        $this->currs["FJD"] = array("N" => "Fiji",                                                 "C" => "Dollars");
        $this->currs["FKP"] = array("N" => "Falkland Islands (Malvinas)",                          "C" => "Pounds");
        $this->currs["GBP"] = array("N" => "United Kingdom",                                       "C" => "Pounds");
        $this->currs["GEL"] = array("N" => "Georgia",                                              "C" => "Lari");
        $this->currs["GGP"] = array("N" => "Guernsey",                                             "C" => "Pounds");
        $this->currs["GHS"] = array("N" => "Ghana",                                                "C" => "Cedis");
        $this->currs["GIP"] = array("N" => "Gibraltar",                                            "C" => "Pounds");
        $this->currs["GMD"] = array("N" => "Gambia",                                               "C" => "Dalasi");
        $this->currs["GNF"] = array("N" => "Guinea",                                               "C" => "Francs");
        $this->currs["GTQ"] = array("N" => "Guatemala",                                            "C" => "Quetzales");
        $this->currs["GYD"] = array("N" => "Guyana",                                               "C" => "Dollars");
        $this->currs["HKD"] = array("N" => "Hong Kong",                                            "C" => "Dollars");
        $this->currs["HNL"] = array("N" => "Honduras",                                             "C" => "Lempiras");
        $this->currs["HRK"] = array("N" => "Croatia",                                              "C" => "Kuna");
        $this->currs["HTG"] = array("N" => "Haiti",                                                "C" => "Gourdes");
        $this->currs["HUF"] = array("N" => "Hungary",                                              "C" => "Forint");
        $this->currs["IDR"] = array("N" => "Indonesia",                                            "C" => "Rupiahs");
        $this->currs["ILS"] = array("N" => "Israel",                                               "C" => "New Shekels");
        $this->currs["IMP"] = array("N" => "Isle of Man",                                          "C" => "Pounds");
        $this->currs["INR"] = array("N" => "India",                                                "C" => "Rupees");
        $this->currs["IQD"] = array("N" => "Iraq",                                                 "C" => "Dinars");
        $this->currs["IRR"] = array("N" => "Iran",                                                 "C" => "Rials");
        $this->currs["ISK"] = array("N" => "Iceland",                                              "C" => "Kronur");
        $this->currs["JEP"] = array("N" => "Jersey",                                               "C" => "Pounds");
        $this->currs["JMD"] = array("N" => "Jamaica",                                              "C" => "Dollars");
        $this->currs["JOD"] = array("N" => "Jordan",                                               "C" => "Dinars");
        $this->currs["JPY"] = array("N" => "Japan",                                                "C" => "Yen");
        $this->currs["KES"] = array("N" => "Kenya",                                                "C" => "Shillings");
        $this->currs["KGS"] = array("N" => "Kyrgyzstan",                                           "C" => "Soms");
        $this->currs["KHR"] = array("N" => "Cambodia",                                             "C" => "Riels");
        $this->currs["KMF"] = array("N" => "Comoros",                                              "C" => "Francs");
        $this->currs["KPW"] = array("N" => "Korea (North)",                                        "C" => "Won");
        $this->currs["KRW"] = array("N" => "Korea (South)",                                        "C" => "Won");
        $this->currs["KWD"] = array("N" => "Kuwait",                                               "C" => "Dinars");
        $this->currs["KYD"] = array("N" => "Cayman Islands",                                       "C" => "Dollars");
        $this->currs["KZT"] = array("N" => "Kazakhstan",                                           "C" => "Tenge");
        $this->currs["LAK"] = array("N" => "Laos",                                                 "C" => "Kips");
        $this->currs["LBP"] = array("N" => "Lebanon",                                              "C" => "Pounds");
        $this->currs["LKR"] = array("N" => "Sri Lanka",                                            "C" => "Rupees");
        $this->currs["LRD"] = array("N" => "Liberia",                                              "C" => "Dollars");
        $this->currs["LSL"] = array("N" => "Lesotho",                                              "C" => "Maloti");
        $this->currs["LTL"] = array("N" => "Lithuania",                                            "C" => "Litai");
        $this->currs["LVL"] = array("N" => "Latvia",                                               "C" => "Lati");
        $this->currs["LYD"] = array("N" => "Libya",                                                "C" => "Dinars");
        $this->currs["MAD"] = array("N" => "Morocco",                                              "C" => "Dirhams");
        $this->currs["MDL"] = array("N" => "Moldova",                                              "C" => "Lei");
        $this->currs["MGA"] = array("N" => "Madagascar",                                           "C" => "Ariary");
        $this->currs["MKD"] = array("N" => "Macedonia",                                            "C" => "Denars");
        $this->currs["MMK"] = array("N" => "Myanmar (Burma)",                                      "C" => "Kyats");
        $this->currs["MNT"] = array("N" => "Mongolia",                                             "C" => "Tugriks");
        $this->currs["MOP"] = array("N" => "Macau",                                                "C" => "Patacas");
        $this->currs["MRO"] = array("N" => "Mauritania",                                           "C" => "Ouguiyas");
        $this->currs["MTL"] = array("N" => "Malta",                                                "C" => "Liri (expires 2008-Jan-31)");
        $this->currs["MUR"] = array("N" => "Mauritius",                                            "C" => "Rupees");
        $this->currs["MVR"] = array("N" => "Maldives (Maldive Islands)",                           "C" => "Rufiyaa");
        $this->currs["MWK"] = array("N" => "Malawi",                                               "C" => "Kwachas");
        $this->currs["MXN"] = array("N" => "Mexico",                                               "C" => "Pesos");
        $this->currs["MYR"] = array("N" => "Malaysia",                                             "C" => "Ringgits");
        $this->currs["MZN"] = array("N" => "Mozambique",                                           "C" => "Meticais");
        $this->currs["NAD"] = array("N" => "Namibia",                                              "C" => "Dollars");
        $this->currs["NGN"] = array("N" => "Nigeria",                                              "C" => "Nairas");
        $this->currs["NIO"] = array("N" => "Nicaragua",                                            "C" => "Cordobas");
        $this->currs["NOK"] = array("N" => "Norway",                                               "C" => "Krone");
        $this->currs["NPR"] = array("N" => "Nepal",                                                "C" => "Nepal Rupees");
        $this->currs["NZD"] = array("N" => "New Zealand",                                          "C" => "Dollars");
        $this->currs["OMR"] = array("N" => "Oman",                                                 "C" => "Rials");
        $this->currs["PAB"] = array("N" => "Panama",                                               "C" => "Balboa");
        $this->currs["PEN"] = array("N" => "Peru",                                                 "C" => "Nuevos Soles");
        $this->currs["PGK"] = array("N" => "Papua New Guinea",                                     "C" => "Kina");
        $this->currs["PHP"] = array("N" => "Philippines",                                          "C" => "Pesos");
        $this->currs["PKR"] = array("N" => "Pakistan",                                             "C" => "Rupees");
        $this->currs["PLN"] = array("N" => "Poland",                                               "C" => "Zlotych");
        $this->currs["PYG"] = array("N" => "Paraguay",                                             "C" => "Guarani");
        $this->currs["QAR"] = array("N" => "Qatar",                                                "C" => "Rials");
        $this->currs["RON"] = array("N" => "Romania",                                              "C" => "New Lei");
        $this->currs["RSD"] = array("N" => "Serbia",                                               "C" => "Dinars");
        $this->currs["RUB"] = array("N" => "Russia",                                               "C" => "Rubles");
        $this->currs["RWF"] = array("N" => "Rwanda",                                               "C" => "Rwanda Francs");
        $this->currs["SAR"] = array("N" => "Saudi Arabia",                                         "C" => "Riyals");
        $this->currs["SBD"] = array("N" => "Solomon Islands",                                      "C" => "Dollars");
        $this->currs["SCR"] = array("N" => "Seychelles",                                           "C" => "Rupees");
        $this->currs["SDG"] = array("N" => "Sudan",                                                "C" => "Pounds");
        $this->currs["SEK"] = array("N" => "Sweden",                                               "C" => "Kronor");
        $this->currs["SGD"] = array("N" => "Singapore",                                            "C" => "Dollars");
        $this->currs["SHP"] = array("N" => "Saint Helena",                                         "C" => "Pounds");
        $this->currs["SLL"] = array("N" => "Sierra Leone",                                         "C" => "Leones");
        $this->currs["SOS"] = array("N" => "Somalia",                                              "C" => "Shillings");
        $this->currs["SPL"] = array("N" => "Seborga",                                              "C" => "Luigini");
        $this->currs["SRD"] = array("N" => "Suriname",                                             "C" => "Dollars");
        $this->currs["STD"] = array("N" => "S&atilde;o Tome and Principe",                         "C" => "Dobras");
        $this->currs["SVC"] = array("N" => "El Salvador",                                          "C" => "Colones");
        $this->currs["SYP"] = array("N" => "Syria",                                                "C" => "Pounds");
        $this->currs["SZL"] = array("N" => "Swaziland",                                            "C" => "Emalangeni");
        $this->currs["THB"] = array("N" => "Thailand",                                             "C" => "Baht");
        $this->currs["TJS"] = array("N" => "Tajikistan",                                           "C" => "Somoni");
        $this->currs["TMM"] = array("N" => "Turkmenistan",                                         "C" => "Manats");
        $this->currs["TND"] = array("N" => "Tunisia",                                              "C" => "Dinars");
        $this->currs["TOP"] = array("N" => "Tonga",                                                "C" => "Pa&apos;anga");
        $this->currs["TRY"] = array("N" => "Turkey",                                               "C" => "New Lira");
        $this->currs["TTD"] = array("N" => "Trinidad and Tobago",                                  "C" => "Dollars");
        $this->currs["TVD"] = array("N" => "Tuvalu",                                               "C" => "Tuvalu Dollars");
        $this->currs["TWD"] = array("N" => "Taiwan",                                               "C" => "New Dollars");
        $this->currs["TZS"] = array("N" => "Tanzania",                                             "C" => "Shillings");
        $this->currs["UAH"] = array("N" => "Ukraine",                                              "C" => "Hryvnia");
        $this->currs["UGX"] = array("N" => "Uganda",                                               "C" => "Shillings");
        $this->currs["USD"] = array("N" => "United States of America",                             "C" => "Dollars");
        $this->currs["UYU"] = array("N" => "Uruguay",                                              "C" => "Pesos");
        $this->currs["UZS"] = array("N" => "Uzbekistan",                                           "C" => "Sums");
        $this->currs["VEB"] = array("N" => "Venezuela",                                            "C" => "Bolivares (expires 2008-Jun-30)");
        $this->currs["VEF"] = array("N" => "Venezuela",                                            "C" => "Bolivares Fuertes");
        $this->currs["VND"] = array("N" => "Viet Nam",                                             "C" => "Dong");
        $this->currs["VUV"] = array("N" => "Vanuatu",                                              "C" => "Vatu");
        $this->currs["WST"] = array("N" => "Samoa",                                                "C" => "Tala");
        $this->currs["XAF"] = array("N" => "Communaut&eacute; Financi&egrave;re Africaine BEAC",   "C" => "Francs");
        $this->currs["XAG"] = array("N" => "Silver",                                               "C" => "Ounces");
        $this->currs["XAU"] = array("N" => "Gold",                                                 "C" => "Ounces");
        $this->currs["XCD"] = array("N" => "East Caribbean",                                       "C" => "Dollars");
        $this->currs["XDR"] = array("N" => "IMF Special Drawing Rights",                           "C" => "XDR");
        $this->currs["XOF"] = array("N" => "Communaut&eacute; Financi&egrave;re Africaine BCEAO",  "C" => "Francs");
        $this->currs["XPD"] = array("N" => "Palladium",                                            "C" => "Ounces");
        $this->currs["XPF"] = array("N" => "Comptoirs Fran&ccedil;ais du Pacifique Francs",        "C" => "CFP Francs");
        $this->currs["XPT"] = array("N" => "Platinum",                                             "C" => "Ounces");
        $this->currs["YER"] = array("N" => "Yemen",                                                "C" => "Rials");
        $this->currs["ZAR"] = array("N" => "South Africa",                                         "C" => "Rand");
        $this->currs["ZMK"] = array("N" => "Zambia",                                               "C" => "Kwacha");
        $this->currs["ZWD"] = array("N" => "Zimbabwe",                                             "C" => "Zimbabwe Dollars");

        // ELIMINATE THE MISMATCHED ITEMS
        $this->norml = $this->normalArrays();
    }

    public function getRates()
    {
        return $this->rates;
    }

    public function getCurrs()
    {
        return $this->currs;
    }

    public function getNormalization()
    {
        return $this->norml;
    }

    public function getError()
    {
        return $this->error;
    }

    public function asTerse()
    {
        return $this->terse;
    }

    public function asVerbose()
    {
        return $this->verbose;
    }

    public function asArray()
    {
        return $this->array;
    }

    public function normalArrays()
    {
        $response = NULL;
        foreach ($this->currs as $code => $x)
        {
            if (!isset($this->rates[$code]))
            {
                $response .= PHP_EOL . "$code IS IN CURRENCIES BUT NOT IN RATES";
                unset($this->currs[$code]);
            }
        }
        if ($response) $response .= PHP_EOL;
        foreach ($this->rates as $code => $x)
        {
            if (!isset($this->currs[$code]))
            {
                $response .= PHP_EOL . "$code IS IN RATES BUT NOT IN CURRENCIES";
                unset($this->rates[$code]);
            }
        }
        return $response;
    }

    public function isCurrency($code)
    {
        if (isset($this->rates[$code]))
        {
            return $this->rates[$code];
        }
        return FALSE;
    }

    public function convert($amount, $source_currency, $result_currency, $decimals=6)
    {
        // NORMALIZE DATA
        $this->array   = array();
        $this->error   = NULL;
        $this->terse   = NULL;
        $this->verbose = NULL;

        $source_currency = trim(strtoupper($source_currency));
        if (!isset($this->currs[$source_currency]))
        {
            $this->error .= "Currency $source_currency not found" . PHP_EOL;
        }

        $result_currency = trim(strtoupper($result_currency));
        if (!isset($this->currs[$result_currency]))
        {
            $this->error .= "Currency $result_currency not found" . PHP_EOL;
        }

        if ($this->error) return FALSE;

        // COMPUTE THE CONVERSION
        $rezult = 1.0 / $this->rates[$source_currency] * $this->rates[$result_currency] * $amount;

        //PREPARE THE RESPONSES
        $this->array
        = array
        ( "$source_currency" => number_format($amount, $decimals, '.', '')
        , "$result_currency" => number_format($rezult, $decimals, '.', '')
        )
        ;

        $this->terse
        = $this->array[$source_currency]
        . $source_currency
        . '='
        . $this->array[$result_currency]
        . "$result_currency"
        ;

        $this->verbose
        = $this->array[$source_currency]
        . ' '
        . $source_currency
        . ' ('
        . $this->currs[$source_currency]['N']
        . ' '
        . $this->currs[$source_currency]['C']
        . ')'
        . ' = '
        . $this->array[$result_currency]
        . ' '
        . $result_currency
        .' ('
        . $this->currs[$result_currency]['N']
        . ' '
        . $this->currs[$result_currency]['C']
        . ')'
        ;
        return (string)$rezult;
    }
}

// DEMONSTRATION SCRIPT
echo '<pre>';
$cc = new CurrencyConverter;

// GET THE SPOT PRICE OF AN OUNCE OF GOLD
echo PHP_EOL . 'THE PRICE OF GOLD IS $' . number_format($cc->convert(1.0, 'XAU', 'USD', 2),2);


// CONVERT 3 BRITISH POUNDS TO US DOLLARS
echo PHP_EOL . $cc->convert(3.0, 'gbp', 'usd', 2);

// IF ANY ERROR OCCURRED
if ($cc->getError()) echo PHP_EOL . $cc->getError();

// SHOW THE THREE RETURN METHODS
echo PHP_EOL . $cc->asTerse();
echo PHP_EOL . $cc->asVerbose();
echo PHP_EOL;  var_dump($cc->asArray());


// SHOW ERROR HANDLING - CONVERT 2 FOOS TO BARS
echo PHP_EOL . $cc->convert(2, 'Foo', 'Bar');

// IF ANY ERROR OCCURRED
if ($cc->getError()) echo PHP_EOL . $cc->getError();

// SHOW THE THREE RETURN METHODS
echo PHP_EOL . $cc->asTerse();
echo PHP_EOL . $cc->asVerbose();
echo PHP_EOL;  var_dump($cc->asArray());


// IF A FORM WAS SUBMITTED
if (!empty($_GET))
{
    $getResult = $cc->convert($_GET['a'], $_GET['f'], $_GET['t']);

    // IF ANY ERROR OCCURRED
    if ($cc->getError()) echo PHP_EOL . $cc->getError();

    // SHOW THE THREE RETURN METHODS
    echo PHP_EOL . $cc->asTerse();
    echo PHP_EOL . $cc->asVerbose();
    echo PHP_EOL;  var_dump($cc->asArray());
}


// CREATE A FORM FOR THE CLIENT
echo '<form>';

echo 'AMOUNT TO CONVERT: ';
echo '<input name="a" />';

echo '<br/>CHOOSE "FROM" CURRENCY';
echo '<br/><select name="f">' . PHP_EOL;
echo '<option value="NUL" selected="selected">CHOOSE CURRENCY</option>' . PHP_EOL;
foreach ($cc->getCurrs() as $code => $arr)
{
    echo '<option value="';
    echo $code;
    echo '">';
    echo $arr['N'];
    echo ' ';
    echo $arr['C'];
    echo '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

echo '<br/>CHOOSE "TO" CURRENCY';
echo '<br/><select name="t">' . PHP_EOL;
echo '<option value="NUL" selected="selected">CHOOSE CURRENCY</option>' . PHP_EOL;
foreach ($cc->getCurrs() as $code => $arr)
{
    echo '<option value="';
    echo $code;
    echo '">';
    echo $arr['N'];
    echo ' ';
    echo $arr['C'];
    echo '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

echo '<input type="submit" />';
echo '</form>';

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial