How to Get Stock Quotes with PHP Using the Yahoo Finance API
For as long as the stock market has been piling up data, there have been programmers looking to use that data. When you search for something like "stock price api" online, however, it can be quite hard to find a good, simple, free service for relaying stock information. I've discovered a fix for that problem and it uses a little PHP and the Yahoo! Finance API.
Here's the good news. There is an easy-to-use PHP library for getting data off the Yahoo! Finance YQL API. Here is a link to the yahoo-finance-api GitHub project.
How It Works
Below is an example of how to get basic information on more than one stock. Pay particular attention to line 6, where an array of stock tickers is passed to the
getQuotes method. Also, I'm including the library into this basic script example using
require 'yahoo-finance-api/lib/YahooFinance/YahooFinance.php'; but you could include this library into your system numerous ways.
What about auto-loading?
Out of the box, this library isn't yet packaged to managed automatically within Composer, but there is no reason it couldn't become part of an auto-loaded environment. The source files would just need to be placed within the appropriate directory in your project.
There are a number of methods available for this library. I've listed them out for your convenience.
Hopefully I've show that it is pretty darn easy to get stock info for your web application or system. Don't forget to checkout the project on GitHub.