Home » PHP25 October 2010

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)

Usage file (yahoostock.php)

Hope this helps. Thanks.

PHP

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed