2014-01-17 95 views
5

我有几个要求列在下面。如何使用雅虎财务获得RealTime股票价格

  1. 获取实时股价没有页面刷新或ajax。 (即雅虎财经,他们获得最新的股票价格没有页面刷新和Ajax调用)
  2. 从尽可能多的获取股票价格作为股市BSE一样,NSC等。

现在使用下面的代码我是能够得到股票价格,但要么我必须刷新页面或打电话给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

据我所知,而无需刷新页面或AJAX可以从服务器使用获取数据http://socket.io/#how-to-use – yashhy

回答

2

好,在你的方法,股价读取由客户端(用户的浏览器)触发。所以没有办法在页面刷新或AJAX之外触发它。

但是,无论用户如何,您的服务器都可以获取这些数据。例如:

data source <----> your backend server fetching the data ---> your database <---- your frontend web server <---> users 

后端和前端服务器可以是相同的服务器,但具有不同的进程。

+0

它只是演示,这就是为什么它看起来像这样。我也可以从ajax使用这个代码,但我也不想使用ajax(你可以在我的需求中看到这个)。 – Dipen

1

至于谷歌是我很确定这个API已经被弃用,并且不再工作。你可以使用雅虎财经api,他们有csv下载和通过yql的api。

参见:https://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs

至于实时的conecrned,我建议看看雅虎网络服务。以下是一个示例: http://finance.yahoo.com/webservice/v1/symbols/ITC.NS,ITC.BO/quote?format=json 如果不提供格式,它将返回一个XML。

如何在没有刷新或Ajax的情况下实时更新?

您可以创建一个pubsub模型并让您的应用程序订阅您的应用程序,但您必须创建此图层,因为yahoo api是基于拉取而不是基于推送的。因此,您需要从雅虎提取股票报价并将它们推送至您的应用程序。你可以使用JMS的Java或套接字,以适合你为准。

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)); 
相关问题