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.
Updates in Release 1.0
- isSuccess method added to YahooFinance_Result object to check if request was successful.
- Bug fixes for typo errors in YahooFinance_Options
- toXML method added to YahooFinance_Result and YahooFinance_Quote
- STRICT and LOOSE mode added to YahooFinanceAPI to throw or supress exceptions
Online Documentation
The YahooFinanceAPI PHP Library is fully documented using phpDocumentor. The online documentation contains a full code example as well as class and method descriiptions and parameters.
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
require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');
Step 2: Register the autoloader
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
$result = $api->getQuotes();
Step 6: Use the Stock Information
if( $result->isSuccess() ) {
$quotes = $result->data;
foreach( $quotes as $quote ) {
$symbol = $quote->symbol;
$lastTrade = $quote->lastTrade;
}
}
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-1.0.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.
Comments & Questions
Add Your Comment