YahooFinanceAPI is an open source php library for interacting with the Yahoo Finance Stock API. For those of you unfamiliar with Yahoo’s Stock API please review my previous post Yahoo Finance Stock API. This library contains an easy interface for obtaining information on one or more stocks. For a quick demo of the library feel free to go to http://mdbitz.com/testing/PHPYahooFinance/finance.php?symbol=DELL which will return JSON coded response of some of the basic information that can be obtained via the library. Also feel free to swap out the symbol in the url with any of your choice, if it is an invalid symbol you will simply not get any information.
How do I use the YahooFinanceAPI library?
The YahooFinanceAPI was constructed so that users need to simply follow a basic 6 set process to get any stock information they are looking for.
Step 1: Obtain the main YahooFinanceAPI class
Before you are able to utilize the YahooFinanceApi library you will need to require or include it into your php script.
require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');
Step 2: Register the autoloader
To mitigate changes in potential future versions this library utilizes an autoloader to load its classes when and if needed. To properly function you will need to register its autoloader by the following command.
spl_autoload_register(array('YahooFinanceAPI', 'autoload'));
Step 3: Identify the information to be returned
Now that the library is properly configured in your script lets define the information that is to be returned for each of the symbols or stocks being looked up. This is done by using the addOption function of the YahooFinanceAPI instance. For a complete list of options please view the YahooFinance_Options class.
$api = new YahooFinanceAPI();
// set options
$api->addOption("symbol");
$api->addOption("previousClose");
$api->addOption("open");
$api->addOption("lastTrade");
$api->addOption("lastTradeTime");
$api->addOption("change" );
$api->addOption("daysLow" );
$api->addOption("daysHigh" );
$api->addOption("volume" );
Step 4: Identity the symbols (stocks) to obtain the information for
After defining the information to be returned we define they symbols (stocks) to be returned. This is done by the addSymbol function. To find symbols you can search Yahoo Finance for the company you are interested in.
$api->addSymbol("DELL" );
Step 5: Query for the Information
At this point we have configured the library and defined the stocks and information that we are looking for and are ready to perform our query of the Yahoo Finance Stock API.
$result = $api->getQuotes();
Step 6: Use the Stock Information
The result object returned contains a code property to tell you if the query was successfull 2XX or failed 4XX. In addition the data property contains an array of the requested information for each of the symbols.
$results->code //-- returns the http response code
$results->data //-- array of Quote Objects
$results->data[0] ->symbol //-- returns the symbol name of the first Quote Object
Download the YahooFinanceAPI Library
The YahooFinanceAPI is copyright MDBitz - Matthew John Denton under the GNU Public License. The current release is available for download at http://mdbitz.com/downloads/YahooFinanceAPI.zip. The archive contains the php classes as well as a test.php file that demonstrates how to use the library.
Feedback Appreciated
Please leave your comments, suggestions, and/or questions below. And if you need any additional support please feel free to send me an email at info@mdbitz.com.
Thanks for posting the article, was certainly a great read!