Len Whistler
03-22-2007, 12:39 AM
This PHP script can be used to get real time stock and currency quotes from Yahoo Finance. The example posted will echo the value of $100 US in Canadian dollars.
The $ticker variable can be currency or stocks. Lots of info can be gathered from this script such as, high/low of the day, open quotes, last trade, time of last trade, volume, etc. Yahoo Finance has all the symbols you need such as RHAT for Red Hat, AAPL for Apple, and all the currency symbols like CADUSD=X for US Dollar and Canadian dollar.
<?php
$us_price = '100';
$ticker= 'CADUSD=X';
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchange_rate = fread($open, 2000);
fclose($open);
$exchange_rate = str_replace("\"", "", $exchange_rate);
$exchange_rate = explode(",", $exchange_rate);
$ca_price = ($us_price/$exchange_rate[1]);
$price = number_format ($ca_price, 2);
echo "$us_price US dollars = \$$price Canadian dollars";
?>
The $ticker variable can be currency or stocks. Lots of info can be gathered from this script such as, high/low of the day, open quotes, last trade, time of last trade, volume, etc. Yahoo Finance has all the symbols you need such as RHAT for Red Hat, AAPL for Apple, and all the currency symbols like CADUSD=X for US Dollar and Canadian dollar.
<?php
$us_price = '100';
$ticker= 'CADUSD=X';
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchange_rate = fread($open, 2000);
fclose($open);
$exchange_rate = str_replace("\"", "", $exchange_rate);
$exchange_rate = explode(",", $exchange_rate);
$ca_price = ($us_price/$exchange_rate[1]);
$price = number_format ($ca_price, 2);
echo "$us_price US dollars = \$$price Canadian dollars";
?>