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.


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.


Let us now fetch stock data from Yahoo! Finance.

You can get stock data from Yahoo from the following URL:

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



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


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.


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)
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


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.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Pingback: Finance Stock Market » Blog Archive » PHP: How to get stock quote data from Yahoo! Finance? (Complete …()

  • Pingback: The BB and Iphone Zone()

  • Pingback: Just how to find out with regards to- Stock exchange with realistic time stock tickers? | OnlineCommisions.com()

  • Pingback: rick()

  • mihaela

    Can anyone help me with an answer: does yahoo provide intraday data like open, high, low, close and volume for each minute? I see on their chart that you can have candlesticks drown on a chart that only covers one day, so they have OHLC minute by minute, hte question is how to access it. Thank you.

  • Soundshining

    how to get roe and roa values?


  • Saurabh

    How do i populate the data into mysql with the above script?

  • Dave

     This is such a simple solution to what I thought would be a lot harder to do. I can’t thank you enough for this little tutorial….

  • Joe

    thanks dude.
    this script was helpful :)

  • FR

    Thank you.

  • Awesome! Thanks much. There is a fair bit of delay (at Yahoo’s end that is) but it works great! Thanks much sir!

  • Pingback: Finding News On A Stock | Penny Stocks to Watch: Microcap Millionaires()

  • is it still working? Today i get no data

  • Joe

     This is a nice script but don’t rely on Yahoo data too much. I’ve been using them for over a year and they really do have tons of bad data. On the other hand, you might be better of fetching the data using yql.

  • Manish

    Thank you !!! your script so good, one query , Please help me

    THis script seem to only display New York Nasdaq price..

    How to get i international share price?


  • awesome and yet simple script, thx for sharing the idea!

  • Lawrence Weiss

    Thanks for taking the time to post this you bacon saver you!

  • Davide

    Hi, great script. I’m only having an issue with Dow Jones indexes, ^DJI, DJA etcetera are not mined. Every other index, like Nasdaq, Nikkei 225 is ok. With Dow Jones no data is grabbed and the array is completely empty. Is it a problem of yahoo finance?

  • ud

    Thank You Very Much for your kind help !

  • Jeet Singha

    Awsome You have saved my life.

  • Mike Strumpet

    Is there a way to add CSS to these values? I’d like to make the percentage change a red for down and green for up.

  • meir

    YQL- question
    I need to get the properties of this table, how the query need to be?

  • mozunk

    I’d update explode with str_getcsv
    $data = str_getcsv($s, “,”, ‘”‘);

  • Hi, Still working well ! but doesnt show dow jones index. Do you have any other solutions or tips?
    Thank you.

  • Ash Schulz

    I love this script and played around with it for a while but cant get real time quotes, it seems yahoo is on a 15 minute delay. is it possible to access the same kind of information directly form the nasdaq website instead?

  • Ángel Hilario Alvarez Esteven

    How to put special characters, example : ^IBEX
    (^) ???

  • Harrison Delfino

    Yahoo finance API is not available anymore. I have moved to MarketXLS after this change, much more reliable data.