PHP: How to get stock quote data from Yahoo! Finance? (Complete Code and Tutorial)

In this tutorial, I will be showing you how you can fetch any company’s data from Yahoo! Finance.

INTRODUCTION

First of all, let us see the stock quote data of top tech companies on Yahoo! Finance.

View the basic Google stock chart on Yahoo! Finance: http://finance.yahoo.com/q?s=goog

For eBay: http://finance.yahoo.com/q?s=EBAY
For Amazon: http://finance.yahoo.com/q?s=AMZN
For Apple: http://finance.yahoo.com/q?s=AAPL
For Microsoft: http://finance.yahoo.com/q?s=MSFT
For Yahoo: http://finance.yahoo.com/q?s=YHOO

Similarly, you can find for others. You just need to change the stock symbol.
All industry list is present here: http://biz.yahoo.com/ic/ind_index.html
You can click on any industry sector from the list and see the stock symbol for industries.

DEMONSTRATION

Let us now fetch stock data from Yahoo! Finance.

You can get stock data from Yahoo from the following URL:
http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format

Here,
$stock = stock symbol (e.g. GOOG for Google, MSFT for Microsoft, APPL for Apple, etc.)
$format = format or tags to be fetched

Example,

http://finance.yahoo.com/d/quotes.csv?s=GOOG+AAPL+MSFT+YHOO&f=snl1d1t1cv

The above URL will fetch stock data of Google, Apple, Microsoft and Yahoo.
It will fetch data for the following parameters:-

s = Symbol
n = Name
l1 = Last Trade (Price Only)
d1 = Last Trade Date
t1 = Last Trade Time
c = Change and Percent Change
v = Volume

FIND STOCK SYMBOLS FOR ANY COMPANY

All industry list is present here: http://biz.yahoo.com/ic/ind_index.html
You can click on any industry sector from the list and see the stock symbol for industries.

FIND 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 – Day’s Low
g1 – Holdings Gain Percent
g3 – Annualized Gain
g4 – Holdings Gain
g5 – Holdings Gain Percent (Real-time)
g6 – Holdings Gain (Real-time)
h – Day’s 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 – Day’s Range
m2 – Day’s 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 – Day’s Value Change
w4 – Day’s Value Change (Real-time)
x – Stock Exchange
y – Dividend Yield

PHP CODE TO FETCH STOCK DATA FROM YAHOO! FINANCE

I have made two files for this purpose. The class file contains class and function to fetch the required data. And the usage file contains code to display it. I have written enough comments in the code for better understanding.

Source credit: Yahoo Stock Report

Class file (class.yahoostock.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;
    }
} 

Usage file (yahoostock.php)


<?php
include_once('class.yahoostock.php');

$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
 */
$objYahooStock->addFormat("snl1d1t1cv"); 

/**
	Add company stock code to be fetched
	
	msft = Microsoft
	amzn = Amazon
	yhoo = Yahoo
	goog = Google
	aapl = Apple	
 */
$objYahooStock->addStock("msft");
$objYahooStock->addStock("amzn");
$objYahooStock->addStock("yhoo");
$objYahooStock->addStock("goog"); 
$objYahooStock->addStock("vgz"); 

/**
 * Printing out the data
 */
foreach( $objYahooStock->getQuotes() as $code => $stock)
{
	?>
	Code: <?php echo $stock[0]; ?> <br />
	Name: <?php echo $stock[1]; ?> <br />
	Last Trade Price: <?php echo $stock[2]; ?> <br />
	Last Trade Date: <?php echo $stock[3]; ?> <br />
	Last Trade Time: <?php echo $stock[4]; ?> <br />
	Change and Percent Change: <?php echo $stock[5]; ?> <br />
	Volume: <?php echo $stock[6]; ?> <br /><br />
	<?php
}
?>

Hope this helps. Thanks.