PHP Stock Market API

27 Comments

Join the Conversation

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:

<?php
require_once('class.stockMarketAPI.php');
?>

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

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

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

<hr>

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

Comments

  1. Christian Zuluaga
    at 8:48 pm

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

  2. Ian
    at 8:41 am

    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

  3. Olaf
    at 4:23 am

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

    • machen
      at 4:02 am

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

  4. Harry Espino
    at 7:59 pm

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

    • machen
      at 4:00 am

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

    • JA
      at 3:10 pm

      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.

  5. machen
    at 4:07 am

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All comments are held for moderation. I'll publish all comments that are on topic and not rude. You'll even get little stars if you do an extra good job.

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.

%d bloggers like this: