php – 如何使用雅虎财务获得RealTime股票价格

前端之家收集整理的这篇文章主要介绍了php – 如何使用雅虎财务获得RealTime股票价格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有几个要求,如下所列.

>获取实时股票价格,无需刷新页面或ajax. (即雅虎财经,他们获得最新的股票价格,没有页面刷新和ajax调用)
>从BSE,NSC等股票市场获取股票价格.

现在使用以下代码我能够获得股票价格,但要么我必须刷新页面调用ajax,在这两种情况下需要20到30秒,但在许多财务网站,他们可以每秒更新价格而不使用ajax.

  1. PHP
  2. /**
  3. * Class to fetch stock data from Yahoo! Finance
  4. *
  5. */
  6. class YahooStock {
  7. /**
  8. * Array of stock code
  9. */
  10. private $stocks = array();
  11. /**
  12. * Parameters string to be fetched
  13. */
  14. private $format;
  15. /**
  16. * Populate stock array with stock code
  17. *
  18. * @param string $stock Stock code of company
  19. * @return void
  20. */
  21. public function addStock($stock)
  22. {
  23. $this->stocks[] = $stock;
  24. }
  25. /**
  26. * Populate parameters/format to be fetched
  27. *
  28. * @param string $param Parameters/Format to be fetched
  29. * @return void
  30. */
  31. public function addFormat($format)
  32. {
  33. $this->format = $format;
  34. }
  35. /**
  36. * Get Stock Data
  37. *
  38. * @return array
  39. */
  40. public function getQuotes()
  41. {
  42. $result = array();
  43. $format = $this->format;
  44. foreach ($this->stocks as $stock)
  45. {
  46. /**
  47. * fetch data from Yahoo!
  48. * s = stock code
  49. * f = format
  50. * e = filetype
  51. */
  52. $s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e=.csv");
  53. /**
  54. * convert the comma separated data into array
  55. */
  56. $data = explode( ',',$s);
  57. /**
  58. * populate result array with stock code as key
  59. */
  60. $result[$stock] = $data;
  61. }
  62. return $result;
  63. }
  64. }
  65. $objYahooStock = new YahooStock;
  66. /**
  67. Add format/parameters to be fetched
  68. s = Symbol
  69. n = Name
  70. l1 = Last Trade (Price Only)
  71. d1 = Last Trade Date
  72. t1 = Last Trade Time
  73. c = Change and Percent Change
  74. v = Volume
  75. */
  76. $objYahooStock->addFormat("snl1d1t1cv");
  77. /**
  78. Add company stock code to be fetched
  79. msft = Microsoft
  80. amzn = Amazon
  81. yhoo = Yahoo
  82. goog = Google
  83. aapl = Apple
  84. */
  85. $objYahooStock->addStock("msft");
  86. $objYahooStock->addStock("amzn");
  87. $objYahooStock->addStock("yhoo");
  88. $objYahooStock->addStock("goog");
  89. $objYahooStock->addStock("vgz");
  90. $objYahooStock->addStock("FB");
  91. /**
  92. * Printing out the data
  93. */
  94. ?>
  95. PHP
  96. foreach( $objYahooStock->getQuotes() as $code => $stock)
  97. {
  98. ?>
  99. PHP //print_r($stock); ?>PHP echo $stock[0]; ?>PHP echo $stock[1]; ?>PHP echo $stock[2]; ?>PHP echo $stock[3]; ?>PHP echo $stock[4]; ?>PHP echo $stock[5]; ?>PHP echo $stock[6]; ?>PHP
  100. }
  101. ?>
  102. 最佳答案
  103. PHP
  104. class U_Yahoo{
  105.     private function file_get_contents_curl($url) {
  106.         $ch = curl_init();
  107.         curl_setopt($ch,CURLOPT_HEADER,0);
  108.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  109.         curl_setopt($ch,CURLOPT_URL,$url);
  110.         $data = curl_exec($ch);
  111.         curl_close($ch);
  112.         return $data;
  113.     }
  114.     //return the history quote from the simbol,default begin date is 90 day ago,the default end is today
  115.     public function getHistoryQuote($symbol,$begin = 90,$end = 0){
  116.         if(!$begin && !$end)
  117.             $begin = $end = 0;
  118.         $begin = Date('Y-m-d',strtotime("-{$begin} days"));
  119.         $end = Date('Y-m-d',strtotime("-{$end} days"));
  120.         $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=";
  121.         $jason_obj = json_decode( $this->file_get_contents_curl($url) );
  122.         return $jason_obj->query->results->quote;
  123.     }
  124.     //return not just the quote but others informations too
  125.     public function getCurrentData($symbol){
  126.         $is_array = is_array($symbol);
  127.         $imp_symbol = ($is_array)? implode('%22%2C%22',$symbol) : $symbol;
  128.         $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=";
  129.         $jason_obj = json_decode( $this->file_get_contents_curl($url) );
  130.         $result = $jason_obj->query->results->quote;
  131.         return (is_array($symbol) and (count($symbol) == 1))? [$result] : $result;
  132.     }
  133.     //return all quotes from the param $symbol passed,if symbol is array,it will return other array indexed by the symbols
  134.     public function getCurrentQuote($symbol){
  135.         if(is_array($symbol)){
  136.             $symbol = empty($symbol)? ['GOOG'] : $symbol;
  137.             $data = $this->getCurrentData($symbol);
  138.             $result = [];
  139.             for ($c = 0; $c < count($data); $c++) { 
  140.                 $result[$data[$c]->Symbol] = $data[$c]->LastTradePriceOnly;
  141.             }
  142.             return $result;
  143.         }else
  144.             return $this->getCurrentData($symbol)->LastTradePriceOnly;
  145.     }
  146. }
  147. 怎么用:

  148. $yahoo = new U_Yahoo();
  149. var_dump( $yahoo->getCurrentQuote('GOOG') );
  150. var_dump( $yahoo->getCurrentQuote(['GOOG','YHOO']) );
  151. var_dump( $yahoo->getCurrentData(['GOOG','YHOO']) );
  152. var_dump( $yahoo->getHistoryQuote('YHOO',20,0) );
    • 猜你在找的jQuery相关文章