digitalpoint
Mar 5th 2004, 8:30 am
I'm sure there is some sort of free XML feed for stock quotes out there, but I wasn't able to find one. Yahoo finance has the ability to download up to date stock data (with the normal 15 minute lag) to a CSV file.
So, let's say we wanted a stock quote for Yahoo... First, go to Yahoo finance's page for whatever company you want a quote for:
http://finance.yahoo.com/q?s=YHOO
Under the stock figures, there is a download data option. Use that URL in the fopen() function below.
echo "Stock quote: ";
$handle = @fopen("http://finance.yahoo.com/d/quotes.csv?s=YHOO&f=sl1d1t1c1ohgv&e=.csv", "r");
$data = @fgetcsv($handle, 1000);
@fclose($handle);
echo "<B>" . $data[1] . "</B> ";
if ($data[4] < 0) {
echo "<FONT COLOR=RED>" . $data[4] . "</FONT>";
} elseif ($data[4] > 0) {
echo "<FONT COLOR=GREEN>" . $data[4] . "</FONT>";
} else {
echo $data[4];
}
echo " (" . date ("M j, g:i a", strtotime($data[2] . " " . $data[3])) . " EST)";
Ta-da! You now have a stock quote that is "live".
- Shawn
So, let's say we wanted a stock quote for Yahoo... First, go to Yahoo finance's page for whatever company you want a quote for:
http://finance.yahoo.com/q?s=YHOO
Under the stock figures, there is a download data option. Use that URL in the fopen() function below.
echo "Stock quote: ";
$handle = @fopen("http://finance.yahoo.com/d/quotes.csv?s=YHOO&f=sl1d1t1c1ohgv&e=.csv", "r");
$data = @fgetcsv($handle, 1000);
@fclose($handle);
echo "<B>" . $data[1] . "</B> ";
if ($data[4] < 0) {
echo "<FONT COLOR=RED>" . $data[4] . "</FONT>";
} elseif ($data[4] > 0) {
echo "<FONT COLOR=GREEN>" . $data[4] . "</FONT>";
} else {
echo $data[4];
}
echo " (" . date ("M j, g:i a", strtotime($data[2] . " " . $data[3])) . " EST)";
Ta-da! You now have a stock quote that is "live".
- Shawn