PHP Stock Quote
Posted: Sun Nov 29, 2009 4:02 am
I made this last night so I thought I'd share. This script rakes yahoo finance for stock quotes and stores the results in an array. Its not extremely sophisticated but it works.
The output array looks like this:
Code: Select all
// quote function
function quote($symbol) {
$htmlfile = "http://finance.yahoo.com/q?s=";
$url = $htmlfile.$symbol;
// original file contents
$original_file = @file_get_contents($url);
// if file_get_contents fails to open the link do nothing
if(!$original_file) {}
else {
$stock = array();
// find company name in original file contents
preg_match_all("/<h1>(.*?)<\/h1>/", $original_file, $name);
$stock['name'] = strip_tags($name[1][0]);
// find table rows in original file contents
preg_match_all("/<tr(.*?)>(.*?)<\/tr>/", $original_file, $matches);
$result = $matches[0];
$tag = "/<td class=\"yfnc_tabledata1\">(.*?)<\/td>/";
// Latest Trade
preg_match_all($tag, $result[0], $value);
$stock['value'] = strip_tags($value[0][0]);
// Trade Time
preg_match_all($tag, $result[1], $time);
$stock['time'] = strip_tags($time[0][0]);
// Previous Close
preg_match_all($tag, $result[2], $previousclose);
$stock['previousclose'] = strip_tags($previousclose[0][0]);
// Open
preg_match_all($tag, $result[3], $open);
$stock['open'] = strip_tags($open[0][0]);
// Change
$stock['change'] = $stock['value']-$stock['open'];
// Bid
preg_match_all($tag, $result[4], $bid);
$stock['bid'] = strip_tags($bid[0][0]);
// Ask
preg_match_all($tag, $result[5], $ask);
$stock['ask'] = strip_tags($ask[0][0]);
// Day's Range
preg_match_all($tag, $result[7], $daysrange);
$stock['daysrange'] = strip_tags($daysrange[0][0]);
// 52 wk Range
preg_match_all($tag, $result[8], $yearrange);
$stock['yearrange'] = strip_tags($yearrange[0][0]);
// Volume
preg_match_all($tag, $result[9], $volume);
$stock['volume'] = strip_tags($volume[0][0]);
// Avg. Volume
preg_match_all($tag, $result[10], $avgvolume);
$stock['avgvolume'] = strip_tags($avgvolume[0][0]);
// Market Cap
preg_match_all($tag, $result[11], $marketcap);
$stock['marketcap'] = strip_tags($marketcap[0][0]);
// P/E
preg_match_all($tag, $result[12], $pe);
$stock['pe'] = strip_tags($pe[0][0]);
// EPS
preg_match_all($tag, $result[13], $eps);
$stock['eps'] = strip_tags($eps[0][0]);
// Dividend & Yeild
preg_match_all($tag, $result[14], $dividend);
$stock['dividend'] = strip_tags($dividend[0][0]);
// Forward P/E
preg_match_all($tag, $result[15], $forwardpe);
$stock['forwardpe'] = strip_tags($forwardpe[0][0]);
// P/S
preg_match_all($tag, $result[16], $ps);
$stock['ps'] = strip_tags($ps[0][0]);
echo '<pre>'; print_r($stock); echo '</pre>';
}
}
quote('msft');
Array
(
[name] => Microsoft Corporation (MSFT)
[value] => 29.22
[time] => Nov 27
[previousclose] => 29.79
[open] => 29.01
[change] => 0.21
[bid] => 22.26 x 100
[ask] => N/A
[daysrange] => 28.75 - 29.39
[yearrange] => 14.87 - 30.14
[volume] => 29,357,892
[avgvolume] => 56,729,100
[marketcap] => 259.45B
[pe] => 18.99
[eps] => 1.54
[dividend] => 0.52 (1.80%)
[forwardpe] => 14.05
[ps] => 4.70
)