PDA

View Full Version : Stock and currency quotes from Yahoo Finance


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";

?>

>ssp-cdr<
06-17-2007, 08:32 PM
Could come in handy, thanks.

Len Whistler
06-20-2007, 09:54 PM
Below is a link to a working demo of the PHP stock/currency script customized for a specific currency, in this case the Cuban Convertible Peso (CUC).

Currency exchange calculator using PHP (http://www.rgbpixels.ca/currency/)