PHP Stock Market API

Here's a quick API script I put together to help monitor my stocks. It's a PHP Stock Market API that uses the Yahoo! Finance API and allows you to retrieve up to 20 current data points for a particular symbol. It's simple to use and doesn't require any knowledge on how to implement or use the Yahoo! Finance API.
Typical Read
2 minutes
Share on facebook
Share on twitter
Share on linkedin
Sponsored Advertisement
Don’t settle for wimpy nutritional drink — Huel-up.

Looking to grab the latest stock market data? There’s a surprising lack of stock market APIs out there and even fewer that work consistently or affordable. Never fear, the PHP Stock Market API is here!

The PHP Stock Market API is a simple PHP class that uses the Yahoo! Finance API allowing you to retrieve up to 20 current data points for a particular symbol. It’s simple to implement and doesn’t require you to learn how to use the Yahoo! API.

UPDATE (Nov. 28, 2014, v1.3): getData now allows an array to pull multiple stocks at once (thanks @d3vit).
UPDATE (Jan. 27, 2014, v1.2): You can now also retrieve historical data (open, high, low, close, volume & adjusted close).

Download Now (Version 1.3) Fork on GitHub

PHP Stock Market API Usage

With just a few lines of code, you can easily retrieve 20 current data points. Here’s how it works:


<h1>Current Stock Information for AAPL</h1>

$StockMarketAPI = new StockMarketAPI;
$StockMarketAPI->symbol = 'AAPL';
<pre><?php print_r($StockMarketAPI->getData());?></pre>

$start = '01-01-2013';
$end = '01-07-2013';
<h1>Historical Stock Information for AAPL (<?php echo $start ?> - <?php echo $end ?>)</h1>
$StockMarketAPI = new StockMarketAPI;
$StockMarketAPI->symbol = 'AAPL';
$StockMarketAPI->history = array(
  'start'    => $start,
  'end'    => $end,
  'interval' => 'd' // Daily
<pre><?php print_r($StockMarketAPI->getData());?></pre>


<h1>Stock Information for AAPL, MSFT, GOOGL</h1>
$StockMarketAPI = new StockMarketAPI;
$StockMarketAPI->symbol = array('AAPL', 'MSFT', 'GOOGL');
<pre><?php print_r($StockMarketAPI->getData());?></pre>
Related Articles

28 Comments on “PHP Stock Market API”

# Jun 22, 2020

Not to hijack the good work that Ben has done here, but here’s another one that is a bit more dynamic:


# Jul 31, 2017

please, i dont know how to make this code function on an interface. somebody help…

Harry Espino

# Jul 9, 2017

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


# Jul 31, 2017

please how could i make this codes function so i could view the interface


# Nov 11, 2017

Are you using MarketXLS (w/Excel) or MarketXLS-API (with your own script). If “your own script” what language did you use? I’m going to start work on a php script.

# Dec 12, 2017

PHP, but the place I was pulling data from (Yahoo) no longer provides access to it.

Andrew Wasson

# Jun 9, 2017

The fix for the change in the API can be found here:


# Jul 31, 2017

i wanna make this codes work, how could i achieve that.


# Jun 9, 2017

Hello, Yahoo has changed API Query, could you perhaps adopt script? Thanks.


# May 30, 2017

I’ve been using this API as the backbone to a personal project to gather stock information and it has worked flawlessly up until this weekend. Worked correctly last on May 26th, 2017.

I have made no changes to my projects code in several months and have had no issues until this weekend and now the API is unable to retrieve data?

Curious if it is still working for others or if Yahoo finally changed their output methods or blocked this type of data requesting? Any information would be helpful, thanks

Christian Zuluaga

# Nov 30, 2016

Great work. This code provides much useful information about stocks.

# Aug 28, 2016

Hi Ben, thanks for this! Working great for me. I can’t seem to find any documentation on changing the interval from “daily” (as you have it) to, say, hourly, or minutes. I’m trying to get the current, or previous day’s intraday data for a stock.



# Jul 16, 2016

Still have the problem with Start/ End Date when using historical data. If I set for symbol = ‘AAPL’:
$start = ’01-01-2013′;
$end = ’31-12-2013′;
I get historical data from 01-01-2013 until now, which is 16-07-2016….Did not find a way to fix this….:-(

Another question:
Is there any way of using this API not only for historical stock, but also for historical currency quotes like ‘EURUSD=X’ or commodities like ‘NG=F’ which would be natural gas. See list of commodities/ currencies at yahoo:

That would really be a big thing….I really would appreciate using this API like that. 🙂
Thanks for support and help.

Martin Catudal

# May 18, 2016

Since Yahoo Finance told us that they will drop the realtime update for the gold price, is it possible to use Kitco for source instead?
I don’t know is it possible to use it with this API without huge modification.

# May 18, 2016

Looks like there’s already a script for Kitco:

Andrew Wasson

# May 18, 2016

That might Gold Feed script could be old information… It looks like they’re trying to sell a service now and finding the actual script is eluding me. I recommend the XML Charts free (or paid) service, providing you follow their terms of use. They provide precious and industrial metal feeds in several currencies.

Andrew Wasson

# May 18, 2016


# Apr 26, 2016

great work man! thanks for sharing, really made my life easier..


# Apr 22, 2016

thanks ben, it work for me.

# Jan 8, 2015

This really works fine!

# Nov 8, 2014

Hey Ben,

This is pretty cool but when I try to save the historical data into a variable; ie: $myhistoricaldata = $StockMarketAPI->getData(), the whole array get printed to the page rather than being saved to my variable. I’m not sure what’s going on.

My code would be as follows:

// Get daily historical data for AAPL
$StockMarketAPI = new StockMarketAPI;
$StockMarketAPI->symbol = 'AAPL';
$StockMarketAPI->history = array(
'start' => '2013-1-1', // yyyy-m-d
'end' => '2013-12-31', // yyyy-m-d
'interval' => 'w' // weekly
$myhistoricaldata = $StockMarketAPI->getData());

Any thoughts? Thanks.

# Nov 8, 2014

Oops, there’s an extra bracket at the end of my code example.

# Nov 5, 2014

Great article and thank you for putting this together.

I forked and made a modification to your source so that the getData function will take an array. On GitHub here:

Feel free to integrate if you’d like.

# Nov 17, 2014

If you submit a pull request, I’ll be happy to integrate the features you add instead of branching off a new one.


# Jul 31, 2017

will it function automatically using the github software?


# Nov 3, 2014

The code actually is broken currently when I attempt to run it. Quick fix is to move

$start = '2013-1-1';
$end = '2013-12-31';

into the actual <?php block instead of having it in it's own separate blocks underneat the . The strange thing is, the code has the , then creates variables for $start and $end, but doesn’t actually display it there. Not sure why it was written like that in the first place. All of this is in the index.php file, and after making that change, the code worked great. Thank you!

# Nov 18, 2014

Thanks for the feedback. Just released a new version (1.3, thanks to @d3vit) that should work better for you. Let me know if you run into any problems.

# Jul 21, 2014

Those codes are real life saver specifically for brokers who usually “misplace” their datas. Thanks Ben!

Leave a Reply

Your email address will not be published. Required fields are marked *

All comments posted on 'PHP Stock Market API' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.