Real Time Data Get From Stock Exchange Using PHP
Real Time Data Get From Stock Exchange Using PHP
<?php /** * Class to fetch stock data from Yahoo! Finance * */ class YahooStock { /** * Array of stock code */ private $stocks = array(); /** * Parameters string to be fetched */ private $format; /** * Populate stock array with stock code * * @param string $stock Stock code of company * @return void */ public function addStock($stock) { $this->stocks[] = $stock; } /** * Populate parameters/format to be fetched
* * @param string $param Parameters/Format to be fetched * @return void */ public function addFormat($format) { $this->format = $format; } /** * Get Stock Data * * @return array */ public function getQuotes() { $result = array(); $format = $this->format; foreach ($this->stocks as $stock) { /** * fetch data from Yahoo! * s = stock code * f = format * e = filetype */ $s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e =.csv"); /** * convert the comma separated data into array */ $data = explode( ',', $s); /** * populate result array with stock code as key */ $result[$stock] = $data; } return $result; } } /*?>*/ $objYahooStock = new YahooStock; /** Add format/parameters to be fetched
s = Symbol n = Name l1 = Last Trade (Price Only) d1 = Last Trade Date t1 = Last Trade Time c = Change and Percent Change v = Volume */?> <table border="1" width="62%"> <tr> <td>Name </td> <td>Change Percent</td> <td> Volume </td> <td>Bid(RT)</td> <td>Ask(RT)</td> <td> Day Low </td> <td> Day High </td> <td> Close </td> </tr> <?php $objYahooStock->addFormat("ncvb3b2ghp"); /** Add company stock code to be fetched intc=Intel corporation dell=dell NYSE= NASD msft = Microsoft amzn = Amazon yhoo = Yahoo goog = Google aapl = Apple */ $objYahooStock->addStock("goog"); $objYahooStock->addStock("yhoo"); $objYahooStock->addStock("aapl"); $objYahooStock->addStock("msft"); $objYahooStock->addStock("dell"); $objYahooStock->addStock("vgz"); $objYahooStock->addStock("ebay"); /** * Printing out the data */ ?>
<?php foreach( $objYahooStock->getQuotes() as $code => $stock) { ?><tr> <td> <?php echo $stock[0]; ?> </td> <td> <?php echo $stock[1]; ?> </td> <td> <?php echo $stock[2]; ?> </td> <td> <?php echo $stock[3]; ?> </td> <td> <?php echo $stock[4]; ?> </td> <td> <?php echo $stock[5]; ?> </td> <td> <?php echo $stock[6]; ?> </td> <td> <?php echo $stock[7]; ?> </td> </tr> <?php } ?> </table> /*
COMPLETE LIST OF PARAMETERS Complete list of parameters that can be fetched from Yahoo.
a Ask a2 Average Daily Volume a5 Ask Size b Bid b2 Ask (Real time) b3 Bid (Real time) b4 Book Value b6 Bid Size c Change and Percent Change c1 Change c3 Commission c6 Change (Real time) c8 After Hours Change (Real time) d Dividend/Share d1 Last Trade Date d2 Trade Date e Earnings/Share e1 Error Indication (returned for symbol changed / invalid) e7 EPS Est. Current Year e8 EPS Est. Next Year e9 EPS Est. Next Quarter f6 Float Shares g Days Low g1 Holdings Gain Percent
g3 Annualized Gain g4 Holdings Gain g5 Holdings Gain Percent (Real time) g6 Holdings Gain (Real time) h Days High i More Info i5 Order Book (Real time) j 52 week Low j1 Market Capitalization j3 Market Cap (Real time) j4 EBITDA j5 Change from 52 Week Low j6 Percent Change from 52 Week Low k 52 week High k1 Last Trade (Real time) with Time k2 Change Percent (Real time) k3 Last Trade Size k4 Change from 52 Week High k5 Percent Change from 52 Week High l Last Trade (with time) l1 Last Trade (without time) l2 High Limit l3 Low Limit m Days Range m2 Days Range (Real time) m3 50 Day Moving Average m4 200 Day Moving Average m5 Change from 200 Day Moving Average m6 Percent Change from 200 Day Moving Average m7 Change from 50 Day Moving Average m8 Percent Change from 50 Day Moving Average n Name n4 Notes o Open p Previous Close p1 Price Paid p2 Change in Percent p5 Price/Sales p6 Price/Book q Ex Dividend Date r P/E Ratio r1 Dividend Pay Date r2 P/E (Real time) r5 PEG Ratio r6 Price/EPS Est. Current Year r7 Price/EPS Est. Next Year
s Symbol s1 Shares Owned s7 Short Ratio t1 Last Trade Time t6 Trade Links t7 Ticker Trend t8 1 Year Target Price v Volume v1 Holdings Value v7 Holdings Value (Real time) w 52 Week Range w1 Days Value Change w4 Days Value Change (Real time) x Stock Exchange y Dividend Yield
*/