我有几个要求,如下所示。
现在使用以下代码我能够获得股票价格,但要么我必须刷新页面或调用ajax,在这两种情况下它需要20到30秒,但在许多财务网站,他们可以每秒更新价格使用ajax。
<?php
/**
* Class to fetch stock data from Yahoo! Finance
*
*/
class YahooStock {
/**
* Array of stock code
*/
private $stocks = array();
/**
* Parameters string to be fetched
*/
private $format;
/**
* Populate stock array with stock code
*
* @param string $stock Stock code of company
* @return void
*/
public function addStock($stock)
{
$this->stocks[] = $stock;
}
/**
* Populate parameters/format to be fetched
*
* @param string $param Parameters/Format to be fetched
* @return void
*/
public function addFormat($format)
{
$this->format = $format;
}
/**
* Get Stock Data
*
* @return array
*/
public function getQuotes()
{
$result = array();
$format = $this->format;
foreach ($this->stocks as $stock)
{
/**
* fetch data from Yahoo!
* s = stock code
* f = format
* e = filetype
*/
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e=.csv");
/**
* convert the comma separated data into array
*/
$data = explode( ',', $s);
/**
* populate result array with stock code as key
*/
$result[$stock] = $data;
}
return $result;
}
}
$objYahooStock = new YahooStock;
/**
Add format/parameters to be fetched
s = Symbol
n = Name
l1 = Last Trade (Price Only)
d1 = Last Trade Date
t1 = Last Trade Time
c = Change and Percent Change
v = Volume
*/
$objYahooStock->addFormat("snl1d1t1cv");
/**
Add company stock code to be fetched
msft = Microsoft
amzn = Amazon
yhoo = Yahoo
goog = Google
aapl = Apple
*/
$objYahooStock->addStock("msft");
$objYahooStock->addStock("amzn");
$objYahooStock->addStock("yhoo");
$objYahooStock->addStock("goog");
$objYahooStock->addStock("vgz");
$objYahooStock->addStock("FB");
/**
* Printing out the data
*/
?>
<table width="100%">
<tr>
<th>Row</th>
<th>Code</th>
<th>Name</th>
<th>Last Trade Price</th>
<th>Last Trade Time</th>
<th>Change and Percent Change</th>
<th>Volume</th>
</tr>
<?php
foreach( $objYahooStock->getQuotes() as $code => $stock)
{
?>
<tr>
<td><?php //print_r($stock); ?></td>
<td><?php echo $stock[0]; ?></td>
<td><?php echo $stock[1]; ?></td>
<td><?php echo $stock[2]; ?></td>
<td><?php echo $stock[3]; ?></td>
<td><?php echo $stock[4]; ?></td>
<td><?php echo $stock[5]; ?></td>
<td><?php echo $stock[6]; ?></td>
</tr>
<?php
}
?>
</table>
答案 0 :(得分:5)
<?php
class U_Yahoo{
private function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//return the history quote from the simbol, default begin date is 90 day ago, the default end is today
public function getHistoryQuote($symbol, $begin = 90, $end = 0){
if(!$begin && !$end)
$begin = $end = 0;
$begin = Date('Y-m-d', strtotime("-{$begin} days"));
$end = Date('Y-m-d', strtotime("-{$end} days"));
$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22$symbol%22%20and%20startDate%20%3D%20%22$begin%22%20and%20endDate%20%3D%20%22$end%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
$jason_obj = json_decode( $this->file_get_contents_curl($url) );
return $jason_obj->query->results->quote;
}
//return not just the quote but others informations too
public function getCurrentData($symbol){
$is_array = is_array($symbol);
$imp_symbol = ($is_array)? implode('%22%2C%22', $symbol) : $symbol;
$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22$imp_symbol%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
$jason_obj = json_decode( $this->file_get_contents_curl($url) );
$result = $jason_obj->query->results->quote;
return (is_array($symbol) and (count($symbol) == 1))? [$result] : $result;
}
//return all quotes from the param $symbol passed, if symbol is array, it will return other array indexed by the symbols
public function getCurrentQuote($symbol){
if(is_array($symbol)){
$symbol = empty($symbol)? ['GOOG'] : $symbol;
$data = $this->getCurrentData($symbol);
$result = [];
for ($c = 0; $c < count($data); $c++) {
$result[$data[$c]->Symbol] = $data[$c]->LastTradePriceOnly;
}
return $result;
}else
return $this->getCurrentData($symbol)->LastTradePriceOnly;
}
}
如何使用:
$yahoo = new U_Yahoo();
var_dump( $yahoo->getCurrentQuote('GOOG') );
var_dump( $yahoo->getCurrentQuote(['GOOG', 'YHOO']) );
var_dump( $yahoo->getCurrentData(['GOOG', 'YHOO']) );
var_dump( $yahoo->getHistoryQuote('YHOO', 20, 0) );
答案 1 :(得分:2)
嗯,在您的方法中,股票价格提取由客户(用户的浏览器)触发。所以没有办法在页面刷新或AJAX之外触发它。
但是,无论用户如何,您的服务器都可以获取这些数据。类似的东西:
data source <----> your backend server fetching the data ---> your database <---- your frontend web server <---> users
后端和前端服务器可以是同一台服务器,但具有不同的进程。
答案 2 :(得分:2)
检查此网站,这可能是您想要的(将其用于实时网络应用程序)
教程
http://blog.nodeknockout.com/post/34243127010/knocking-out-socket-io
答案 3 :(得分:1)
就谷歌而言,我很确定该API已被弃用且不再有效。你可以使用Yahoo finance api,他们有api用于csv下载和通过yql。
参考:https://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs
就实时情况而言,我建议看一下雅虎网络服务。以下是一个例子: http://finance.yahoo.com/webservice/v1/symbols/ITC.NS,ITC.BO/quote?format=json 如果您不提供格式,它将返回XML。
如果没有刷新或Ajax,你会如何实现?
您可以创建pubsub模型并让您的应用程序订阅您的应用程序,您必须创建此层,因为yahoo api是基于拉取而不是基于推送。因此,您需要从雅虎提取股票报价并将其推送到您的应用程序。您可以将JMS用于java或套接字,无论哪种适合您。