http://www.lfyzjck.com/yahoo-finance-api/
最近要做的一个网站需要在首页显示世界各大股票交易场所的综合指数,于是搜集了点这方面的资料,今天把研究的结果放出来。
提供财经方面讯息,做的比较好的应该是Google财经和Yahoo财经了,综合考虑还是Yahoo的接口比较好用
API使用方法比较简单,举个例子:
http://finance.yahoo.com/d/quotes.csv?s=^XAU&f=snd1l1c6
返回数据:
”^XAU”,”PHLX Gold/Silver “,”11/24/2010″,213.51,”+1.01″
很典型的CSV数据,比较好处理。“s”指的是股票的代码(Symbol),“f”是要获取的数据选项,上面例子返回的数据分别是“代码”,“股票名称”,“最后更新时间”,“指数”,“变动”。完整的参数列表参见下面表格(对财经方面知道不多,所以不翻译了,以免误导别人):
a | Ask | a2 | Average Daily Volume | a5 | Ask Size |
b | Bid | b2 | Ask (Real-time) | b3 | Bid (Real-time) |
b4 | Book Value | b6 | Bid Size | c | Change & Percent Change |
c1 | Change | c3 | Commission | c6 | Change (Real-time) |
c8 | After Hours Change (Real-time) | d | Dividend/Share | d1 | Last Trade Date |
d2 | Trade Date | e | Earnings/Share | e1 | Error Indication (returned for symbol changed / invalid) |
e7 | EPS Estimate Current Year | e8 | EPS Estimate Next Year | e9 | EPS Estimate Next Quarter |
f6 | Float Shares | g | Day’s Low | h | Day’s High |
j | 52-week Low | k | 52-week High | g1 | Holdings Gain Percent |
g3 | Annualized Gain | g4 | Holdings Gain | g5 | Holdings Gain Percent (Real-time) |
g6 | Holdings Gain (Real-time) | i | More Info | i5 | Order Book (Real-time) |
j1 | Market Capitalization | j3 | Market Cap (Real-time) | j4 | EBITDA |
j5 | Change From 52-week Low | j6 | Percent Change From 52-week Low | k1 | Last Trade (Real-time) With Time |
k2 | Change Percent (Real-time) | k3 | Last Trade Size | k4 | Change From 52-week High |
k5 | Percebt Change From 52-week High | l | Last Trade (With Time) | l1 | Last Trade (Price Only) |
l2 | High Limit | l3 | Low Limit | m | Day’s Range |
m2 | Day’s Range (Real-time) | m3 | 50-day Moving Average | m4 | 200-day Moving Average |
m5 | Change From 200-day Moving Average | m6 | Percent Change From 200-day Moving Average | m7 | Change From 50-day Moving Average |
m8 | Percent Change From 50-day Moving Average | n | Name 股票名称 | n4 | Notes |
o | Open | p | Previous Close | p1 | Price Paid |
p2 | Change in Percent | p5 | Price/Sales | p6 | Price/Book |
q | Ex-Dividend Date | r | P/E Ratio | r1 | Dividend Pay Date |
r2 | P/E Ratio (Real-time) | r5 | PEG Ratio | r6 | Price/EPS Estimate Current Year |
r7 | Price/EPS Estimate Next Year | s | Symbol 股票代码 | s1 | Shares Owned |
s7 | Short Ratio | t1 | Last Trade Time | t6 | Trade Links |
t7 | Ticker Trend | t8 | 1 yr Target Price | v | Volume |
v1 | Holdings Value | v7 | Holdings Value (Real-time) | w | 52-week Range |
w1 | Day’s Value Change | w4 | Day’s Value Change (Real-time) | x | Stock Exchange |
y | Dividend Yield |
上面接口虽然好用,不过根据项目需求,在进行了进一步封装,自己写了个Widget类,方便在模板里调用
class FinanceWidget
{
public $stocks = array();
public $htmlOptions = array();
public $data = array();
public function __construct($data = array(),$htmlOptions = array()){
$this->stocks = $data;
$this->htmlOptions = $htmlOptions;
}
/**
* 渲染ul标签的开始部分
* @param array $htmlOptions
*/
private function _renderHead($htmlOptions){
$options = '';
foreach($htmlOptions as $option => $value){
$options .= "$option=\"$value\" ";
}
echo "
- \n"
;
}
/**
* 渲染主体部分
*/
private
function
_renderBody
(
$data
){
foreach
(
$data
as
$row
){
echo
'
- '.$row[3].''.$row[1].''.$row[2].''."\n";
}
}
/**
* 渲染ul的结束标签
*/
private function _renderFoot(){
echo "
\n";
}
/**
* 返回请求的API
*/
private function getAPI(){
$IDs = join(',',$this->stocks);
return "http://finance.yahoo.com/d/quotes.csv?s=$IDs&f=nl1p2";
}
/**
* 执行渲染
*/
public function run(){
$this->getStocks();
$this->_renderHead($this->htmlOptions);
$this->_renderBody($this->data);
$this->_renderFoot();
}
/**
* 获取股票数据
*/
public function getStocks(){
$row = 0;
$stocks_name = array_keys($this->stocks);
if (($handle = fopen($this->getAPI(), 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
array_push($data,$stocks_name[$row]);
array_push($this->data,$data);
$row++;
}
fclose($handle);
}
}
}
调用的时候只需要定义你需要展示的股票,传给FinanceWidget类:
$stock = array(
'上证指数'=>'000001.ss',
'深圳成指'=>'399001.sz',
'香港恒生'=>'0011.hk',
'日经指数'=>'^N225',
'英国FTSE'=>'^FTSE',
'法国CAC'=>'^FCHI',
'德国DAX'=>'^GDAXI',
'纳斯达克'=>'^IXIC',
'道琼指数'=>'INDU',
);
$widget = new FinanceWidget($metals);
$widget->run();
输出结果如下:
当然这个类支持指定ul的html选项,作为第二个参数传递给Widget类即可,这里不多介绍,大家根据自己需要扩展即可