The purpose of this short tutorial is to answer the question: How do I get stock quotes using the YFaPi Library? The YFaPi PHP Library was constructed to be easily used following a simple 7 step process, 8 if you count using the data.
- Include the main YahooFinanceAPI class
require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');
- Register the autoloader
spl_autoload_register(array('YahooFinanceAPI', 'autoload'));
- Instantiate the YahooFinanceAPI class
$api = new YahooFinanceAPI();
- Identify the information to be returned for each stock
This is done by using the addOption function of the YahooFinanceAPI instance. For a complete list of options please view the online documentation for the YahooFinance_Options class.$api->addOption("symbol"); // or $api->addOption( YahooFinance_Options::SYMBOL ); $api->addOption("previousClose"); // or $api->addOption( YahooFinance_Options::PREVIOUS_CLOSE); $api->addOption("open"); // or $api->addOption( YahooFinance_Options::OPEN ); $api->addOption("lastTrade"); // or $api->addOption( YahooFinance_Options:LAST_TRADE ); $api->addOption("lastTradeTime"); // or $api->addOption( YahooFinance_Options::LAST_TRADE_TIME ); $api->addOption("change" ); // or $api->addOption( YahooFinance_Options::CHANGE ); $api->addOption("daysLow" ); // or $api->addOption( YahooFinance_Options::DAYS_LOW ); $api->addOption("daysHigh" ); // or $api->addOption( YahooFinance_Options::DAYS_HIGH ); $api->addOption("volume" ); // or $api->addOption( YahooFinance_Options::VOLUME );
- Identify the stocks (by their symbols) to obtain the information for
The addSymbol function is used to add the stocks whose quotes you want to obtain. To find symbols you can search Yahoo Finance for the company you are interested in.$api->addSymbol("DELL" );
- Query for the Information
$result = $api->getQuotes();
- Use the Stock Information
if( $result->isSuccess() ) { $quotes = $result->data; foreach( $quotes as $quote ) { $symbol = $quote->symbol; $lastTrade = $quote->lastTrade; // or $quote->get( YahooFinance_Options::LAST_TRADE ); $daysLow = $quote->daysLow; // or $quote->get( YahooFinance_Options::DAYS_LOW ); } }
Full Example
require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');
spl_autoload_register(array('YahooFinanceAPI', 'autoload'));
$api = new YahooFinanceAPI();
$api->addOption("symbol"); // or $api->addOption( YahooFinance_Options::SYMBOL );
$api->addOption("previousClose"); // or $api->addOption( YahooFinance_Options::PREVIOUS_CLOSE);
$api->addOption("open"); // or $api->addOption( YahooFinance_Options::OPEN );
$api->addOption("lastTrade"); // or $api->addOption( YahooFinance_Options:LAST_TRADE );
$api->addOption("lastTradeTime"); // or $api->addOption( YahooFinance_Options::LAST_TRADE_TIME );
$api->addOption("change" ); // or $api->addOption( YahooFinance_Options::CHANGE );
$api->addOption("daysLow" ); // or $api->addOption( YahooFinance_Options::DAYS_LOW );
$api->addOption("daysHigh" ); // or $api->addOption( YahooFinance_Options::DAYS_HIGH );
$api->addOption("volume" ); // or $api->addOption( YahooFinance_Options::VOLUME );
$api->addSymbol("DELL" );
$result = $api->getQuotes();
if( $result->isSuccess() ) {
$quotes = $result->data;
foreach( $quotes as $quote ) {
$symbol = $quote->symbol;
$lastTrade = $quote->lastTrade; // or $quote->get( YahooFinance_Options::LAST_TRADE );
$daysLow = $quote->daysLow; // or $quote->get( YahooFinance_Options::DAYS_LOW );
}
}
Comments & Questions
Add Your Comment