2023-05-12 开启多语言插件支持……

PHP通过Yahoo Finance API获取股票数据

php 苏 demo 2636℃ 0评论

<?php

class FinanceWidget{
   public $stocks = array();
   public $htmlOptions = array();
   public $data = array();


   public function __construct($data = array(),$htmlOptions = array()){
       $this->stocks = $data;
       $this->htmlOptions = $htmlOptions;
   }

  
   private function _renderHead($htmlOptions){
       $options = ”;
       foreach($htmlOptions as $option => $value){
           $options .= “$option=\”$value\” “;
       }
       echo “<ul $options>\n”;
   }

  
   private function _renderBody($data){
       foreach($data as $row){
           echo ‘<li><span>’.$row[3].'</span><span>’.$row[1].'</span><span>’.$row[2].'</span></li>’.”\n”;
       }
   }

  
   private function _renderFoot(){
       echo “</ul>\n”;
   }

  
   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);
       }
   }
}

$stock = array(
   ‘国腾电子’=>’300101.sz’,
   ‘超图软件’=>’300036.sz’,
   ‘数字政通’=>’300075.sz’,
   ‘北斗星通’=>’002151.sz’,
   ‘合众思壮’=>’002383.sz’,
   ‘四维图新’=>’002405.sz’,
   ‘高德软件’=>’AMAP’,
   ‘中国信息技术’=>’CNIT’,
   ‘北大千方’=>’CTFO’,
);
$widget = new FinanceWidget($stock);
$widget->run();
?>

打赏

转载请注明:苏demo的别样人生 » PHP通过Yahoo Finance API获取股票数据

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)