A PHP class to query the Yahoo Finance API.
- search for symbol via Yahoo.Finance.Symbol.AutoSuggest
- query current quotes for symbols
- query historical quotes for single symbol
- query intraday quotes for single symbol
- query basic stock info
- query list of related stocks for index symbols
- query full list of sectors with related industries
You can test an example.php at http://code.dirkolbrich.de/YahooFinanceQuery/
require via composer.json
in your project root
{
"require": {
"dirkolbrich/yahoo-finance-query": "dev-master"
}
}
As simple as that:
use DirkOlbrich\YahooFinanceQuery\YahooFinanceQuery;
// [...]
$query = new YahooFinanceQuery;
or as static:
use DirkOlbrich\YahooFinanceQuery\YahooFinanceQuery;
// [...]
YahooFinanceQuery->make();
YahooFinanceQuery can be configured to return the data as an array or the raw json. Default returnType
is array
.
The config setting has to be passed as an array array('returnType' => 'array')
or array('returnType' => 'json')
.
At initialisation:
$query = new YahooFinanceQuery(array('returnType' => 'json'));
or as static:
YahooFinanceQuery->make(array('returnType' => 'json'));
At run-time:
$query = new YahooFinanceQuery;
$query->config(array('returnType' => 'json'));
The current config setting can be retrieved with:
$query->getConfig();
To retrieve the raw cURL result use raw()
as addition to the query. This method must be called before the actural query method.
$query->raw()->quote()->get();
To force the query via the YQL api (if possible), use the yql()
method within the query string. This method must be called before the actural query method. The default is set to not use YQL, as I think YQL querys are unreliable and often with truncated results.
$query->yql()->quote()->get();
Querys are chainable. Use the get()
method to to retrieve the results.
$data = $query->method()->get();
The following query methods are available:
-
symbolSuggest($string)
Query for symbol suggestion via the YAHOO.Finance.SymbolSuggest.ssCallback
$string = 'basf'; $data = $query->symbolSuggest($string)->get();
-
quote(array $symbol [, array $params])
Query for current quote for given symbols and given parameters.
The passed parameter
$symbol
must be an array. Several symbols can be passed.The passed parameter
$params
is optional and must be an array too. It accepts the parameters as a written word or as tags. See as reference the$quoteParams
variable in the class definition. If$params
is empty, the query will use all possible params.The params 'Symbol', 'LastTradeTime' and 'LastTradeDate' will be quered by default. There will be a unified UTC 'LastTradeTimestamp' added to the result array.
$symbol = array('bas.de'); $params = array('LastTradePriceOnly', 'x', 'c1'); $data = $query->quote($symbol, $params)->get();
-
historicalQuote(array $symbol [, $startDate, $endDate, $param])
Query for historical quotes for given symbol with given start date and end date.
Only one
$symbol
can be passed per query and must be a string.$startDate
and$endDate
must be in the format YYYY-MM-DD. If no dates are passed, the query will grab all available historical quotes. If only one date is passed, the other one will be set to the maximum available.$param
is set to defaultd
= daily. See$historicalQuoteParams
variable for other options.$symbol = array('bas.de'); $startDate = 2013-07-26; $endDate = 2014-01-06; $param = 'd'; $data = $query->historicalQuote($symbol, $startDate, $endDate, $param)->get();
I recommend not to use the
yql()
method with historical quotes, as the YQL console permits only up to 365 single result quotes. To retrieve a full set of historical quotes will not be possible via YQL. -
intraDay($symbol [, $period, $param])
Query finance.yahoo.com for intraday quotes. The symbol must be passed as as string.
$period
is optional and default set to1d
. It is possible to retrieve intraday quotes for up to the last 15 days.$param
is optional and default set toquote
. For other options see the$intraDayParams
variable.$symbol = 'bas.de'; $period = '5d'; $data = $query->intraDay($symbol, $period)->get();
-
stockInfo($symbol)
Query finance.yahoo.com for basic stock information. The symbol must be passed as as string.
$symbol = 'bas.de'; $data = $query->stockInfo($symbol)->get();
-
indexList(array $symbol)
Query for an index which returns the symbol and name of the components. Several symbols may be passed as an array.
See http://finance.yahoo.com/intlindices?e=europe for more symbols to world indices. The caret
^
character must be part of the symbol.$symbol = array('^GDAXI'); $data = $query->indexList($symbol)->get();
-
sectorList()
Query for a complete list of sectors and their corresponding industries.
This function is static without any params.
$data = $query->sectorList()->get();
Which returns an array in this form:
Array ( [0] => Array ( [name] => Basic Materials [industries] => Array ( [0] => Array ( [name] => Agricultural Chemicals [id] => 112 ) [1] => Array ( [name] => Aluminum [id] => 132 ) ... ) ) [1] => Array ( [name] => Conglomerates [industries] => Array ( ... ) )
Some informative blog post and websites:
Yahoo YQL homepage
Blog post on that matter
Blog post from Joseph D. Purcell with an overview over finance API'S is a good entry point
www.gummy-stuff.org explains the Yahoo Finance .csv API
- http://www.gummy-stuff.org/Yahoo-data.htm Update on this: Yahoo harrased Mr. Ponzo about his documentation of a publicy available API and claimed some infringement on their TOS. But thanks to the internet archive, the last snapshot of this site is available at http://web.archive.org/web/20140527154306/http://www.gummy-stuff.org/Yahoo-data.htm
An overview over different api endpoints by Matthias Brusdeylins (german)
Tutorial for the YQL console by Thomas Belser (german)