Explorar o código

合并sina,源文件暂时保留以防有问题

teddy %!s(int64=4) %!d(string=hai) anos
pai
achega
2bed24a8d5

+ 281 - 0
lib/input_sina.class.php

@@ -0,0 +1,281 @@
+<?php
+
+
+class Input_sina
+{
+    const INPUT_SINA_URL = "http://hq.sinajs.cn/list=";
+
+
+    static public function add_day_k($code, $exchange)
+    {
+        $data = self::_parse_day_k_data($code,$exchange);
+        return Day_k::add($data);
+    }
+
+    /***
+     * @param $code
+     * @param $date
+     * @return mixed
+     * 日k线表 day_k_******(每月存一张表,比如boniu_day_k_202011)
+     * 抓取当天的数据,结合腾讯的接口,每4秒抓取一条数据
+     *
+     */
+    static public function add_to_immediate_day_k($code, $exchange)
+    {
+        $data = self::_parse_day_k_data($code,$exchange);
+        return Immediate_base::add($data);
+    }
+
+    private static function _parse_day_k_data($code,$exchange)
+    {
+        $stock_code = $exchange . $code;
+        $url = self::INPUT_SINA_URL . $stock_code;
+        $content3 = self::_render_k_day_curl_data($url);
+
+        if (empty($content3)) {
+            throw new Exception('抓取的数据不能为空', 102);
+        }
+        $data['timestamp'] = time();
+        $data['date'] = (int)date("Ymd", time());
+        $data['code'] = $code;//股票代码此时去掉sh,sz
+        $data['name'] = $content3[0];
+        $data['open_price'] = floatval($content3[1]);//开盘价
+        $data['close_price'] = floatval($content3[3]);//3点之后的当前价格就是收盘价
+        $data['highest_price'] = floatval($content3[4]);//今日最高价
+        $data['lowest_price'] = floatval($content3[5]);//今日最低价
+        $data['amount'] = floatval($content3[8]);//成交的股票数,以百为单位
+        $data['value'] = floatval($content3[9]);//成交金额以万为单位
+        $dataContent = self::_get_tecent_stock_data($code,$exchange);
+        $data["increase_price"] = empty($content1["increase_price"]) ? 0 : $content1["increase_price"]; //  涨跌价
+        $data["increase_ratio"] = empty($content1["increase_ratio"]) ? 0 : $content1["increase_ratio"];//   涨跌幅
+        $data["turnover"] = empty($content1["turnover"]) ? 0 : $content1["turnover"];//    换手率
+        $data["pe_ttm"] = empty($dataContent["pe_ttm"]) ? 0 : $dataContent["pe_ttm"];//pe_ttm市盈率
+        $data["tradable_value"] = empty($dataContent["tradable_value"]) ? 0 : (int)($dataContent["tradable_value"] * 100000000);//流通市值
+        $data["total_value"] = empty($dataContent["total_value"]) ? 0 : (int)($dataContent["total_value"] * 100000000);//总市值
+        $data["pb"] = empty($dataContent["pb"]) ? 0 : $dataContent["pb"];//市净率
+        $data["pe_dynamic"] = empty($dataContent["pe_dynamic"]) ? 0 : $dataContent["pe_dynamic"]; //动态市盈率
+        $data["pe_static"] = empty($dataContent["pe_static"]) ? 0 : $dataContent["pe_static"];//静态市盈率
+        return $data;
+    }
+
+    static private function _render_k_day_curl_data($url)
+    {
+        $content = self::curl_exec($url);
+        $content2 = iconv("gbk", "utf-8", $content);//子串
+        $content3 = substr($content2, stripos($content2, "=\"") + 2); //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510,  ="之后部分
+        $content4 = substr($content3, 0, strripos($content3, "\";")); //获取     万 科A,30.94,0.14,0.45,584039,178827";         ";之前部分
+        $content5 = explode(",", $content4);//分割
+        return $content5;
+    }
+    private static function _get_tecent_stock_data($code, $exchange)
+    {
+
+        $dataContent = Input_tencent_day_k::get_stock_data($code, $exchange);
+        if (!empty($dataContent)) {
+            return $dataContent;
+        }else{
+            return [];
+        }
+
+    }
+    //获取index_day_k
+    static public function getIndexDayK($indexList)
+    {
+        $url = self::INPUT_SINA_URL;
+        $contentList = self::_render_index_k_day_curl_data($url, $indexList);
+        foreach ($contentList as $key => $item) {
+            $data['code'] = $item['code'];
+            $data['date'] = ConverseDate($item[30]);
+            $data['name'] = trim($item[0]);
+            $data['increase_price'] = floatval($item[3] - $item[2]);    //涨跌价
+            $data['timestamp'] = strtotime($item[30]);                       //当天的0点时间戳
+            $data['open_price'] = floatval($item[1]);                        //开盘价
+            $data['close_price'] = floatval($item[3]);                       //当前价,15点之后就是收盘价
+            $data['highest_price'] = floatval($item[4]);                     //最高价
+            $data['lowest_price'] = floatval($item[5]);                      //最低价
+            $data['amount'] = self::getAmountForIndexDayK($data['code'], (int)($item[8]));                              //成交量,以手为单位
+            $data['value'] = (int)($item[9]);                                //成交金额,以万为单位
+
+            $data['increase_ratio'] = 0;                                     //涨跌幅
+            if (!empty($data['open_price']))
+                $data['increase_ratio'] = round($data['increase_price'] * 100 / $data['open_price'], 2);
+
+            if (!empty($item[0])) {            //判断非法股票代码
+                Index_day_k::addOrUpdateByCodeDate($data);
+            }
+        }
+        return;
+    }
+
+    //获取全部股票代码和名称列表
+    static public function getAllStockCodeAndNameList()
+    {
+        //上证主板 60****
+        for($i = 0; $i < 50; $i++) {
+            $start = 600000 + $i * 200;
+            self::getPartStockCodeAndNameList('sh', 10, $start, $start + 200);
+        }
+
+        //上证科创板  688***
+        for($i = 0; $i < 5; $i++) {
+            $start = 688000 + $i * 200;
+            self::getPartStockCodeAndNameList('sh', 11, $start, $start + 200);
+        }
+
+        //深证主板 000000~001999
+        for($i = 0; $i < 10; $i++) {
+            $start = 0 + $i * 200;
+            self::getPartStockCodeAndNameList('sz', 20, $start, $start + 200);
+        }
+
+        //深证中小板 002000~002999
+        for($i = 0; $i < 5; $i++) {
+            $start = 2000 + $i * 200;
+            self::getPartStockCodeAndNameList('sz', 21, $start, $start + 200);
+        }
+
+        //深证创业板 300***
+        for($i = 0; $i < 5; $i++) {
+            $start = 300000 + $i * 200;
+            self::getPartStockCodeAndNameList('sz', 22, $start, $start + 200);
+        }
+
+        return;
+    }
+
+    //获取指定条件的股票代码和名称列表
+    static public function getAllIndexCodeAndNameList()
+    {
+        $i = 0;
+        $index[$i]['code']     = '000001';
+        $index[$i]['name']     = '上证指数';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
+        $index[$i]['sector']   = STOCK_SECTOR_SH_MAIN;
+
+        $i++;
+        $index[$i]['code']     = '399001';
+        $index[$i]['name']     = '深证成指';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
+        $index[$i]['sector']   = STOCK_SECTOR_SZ_MAIN;
+
+        $i++;
+        $index[$i]['code']     = '000016';
+        $index[$i]['name']     = '上证50';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
+        $index[$i]['sector']   = STOCK_SECTOR_SH_MAIN;
+
+        $i++;
+        $index[$i]['code']     = '399300';
+        $index[$i]['name']     = '沪深300';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
+        $index[$i]['sector']   = STOCK_SECTOR_SZ_MAIN;
+
+        $i++;
+        $index[$i]['code']     = '399006';
+        $index[$i]['name']     = '创业板指';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
+        $index[$i]['sector']   = STOCK_SECTOR_SZ_CHUANGYE;
+
+        $i++;
+        $index[$i]['code']     = '000688';
+        $index[$i]['name']     = '科创50';
+        $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
+        $index[$i]['sector']   = STOCK_SECTOR_SH_KECHUANG;
+
+        foreach ($index as $key => $item) {
+            if (!empty($item['code'])) {                            //判断非法股票代码
+                Index::addOrUpdateByCode($item);
+            }
+        }
+        return;
+    }
+    //获取指定条件的股票代码和名称列表
+    static private function getPartStockCodeAndNameList($exchange, $sector, $start, $end)
+    {
+        $data['exchange'] = $exchange;
+        $data['sector'] = $sector;
+        $codeList = array();
+        $codeIndex = 0;
+        $url = self::INPUT_SINA_URL;
+        for($code = $start;$code < $end; $code++){
+            $codeStr = sprintf('%06s', $code);
+            $url .= 's_'.$exchange.$codeStr.',';
+            $codeList[$codeIndex] = $codeStr;
+            $codeIndex++;
+        }
+        $contentList = self::_render_index_k_day_curl_data($url, $codeList);
+        foreach ($contentList as $key => $item) {
+            $data['code'] = $item['code'];
+            $data['name'] = trim($item[0]);
+            if (!empty($item[0])) {                            //判断非法股票代码
+                Stock::addOrUpdateByCode($data);
+            }
+        }
+        return;
+    }
+    static private function _render_index_k_day_curl_data($url,$indexList)
+    {
+        $content = self::curl_exec($url);
+        $codeList = array();
+        $codeIndex = 0;
+        foreach ($indexList as $item) {
+            $codeStr = sprintf('%06s', $item['code']);
+            $url .= $item['exchange'].$codeStr.',';
+            $codeList[$codeIndex] = $codeStr;
+            $codeIndex++;
+        }
+        if (empty($codeList)) {
+            return 0;
+        }
+        $content2 = iconv("gbk", "utf-8", $content);//子串
+        $arr1 =  explode(";", $content2);//按股票分割
+        $arr2 = array();
+        $index = 0;
+        $codeIndex = 0;
+        foreach ($arr1 as $key => $item) {
+            $tempItem = substr($item, stripos($item, "=\"") + 2);         //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510,  ="之后部分
+            $tempItem = substr($tempItem, 0, strripos($tempItem, "\""));  //获取     万 科A,30.94,0.14,0.45,584039,178827";         ";之前部分
+            $tempItem = trim($tempItem);
+            if (!empty($tempItem)) {
+                $arr2[$index] = explode(",", $tempItem);//分割
+                $arr2[$index]['code'] = $codeList[$codeIndex];
+                $index++;
+            }
+            $codeIndex++;
+        }
+
+        return $arr2;
+    }
+
+    //深市返回的成交量以个为单位,需转换为以手为单位
+    static private function getAmountForIndexDayK($code, $amount)
+    {
+        $newAmount = $amount;
+        $index = Index::getInfoByCode($code);
+        if ($index['exchange'] == STOCK_EXCHANGE_SZ) {
+            $newAmount = round($amount / 100);
+        }
+        return $newAmount;
+    }
+
+
+    /****
+     * @param $url
+     * @return array|bool|string
+     * 对curl_setopt进行封装,消除代码冗余
+     */
+    static private function curl_exec($url)
+    {
+        $curlHandle = curl_init();
+        curl_setopt($curlHandle, CURLOPT_URL, $url);
+        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
+        $content = curl_exec($curlHandle);
+        curl_close($curlHandle);//防止中文乱码
+
+        return $content;
+    }
+
+}

+ 1 - 1
task/task_get_all_index_code_name.php

@@ -8,6 +8,6 @@
  */
 require('../init.php');
 
-Input_sina_simple::getAllIndexCodeAndNameList();
+Input_sina::getAllIndexCodeAndNameList();
 
 ?>

+ 1 - 1
task/task_get_all_stock_code_name.php

@@ -8,6 +8,6 @@
  */
 require('../init.php');
 
-Input_sina_simple::getAllStockCodeAndNameList();
+Input_sina::getAllStockCodeAndNameList();
 
 ?>

+ 1 - 1
task/task_get_day_k_stock.php

@@ -10,7 +10,7 @@ require('../init.php');
 $codeList=Stock::getAllList();//获取股票代码
 
 foreach ($codeList as $key=>$value){
-    Input_sina_day_k::get_day_k($value["code"],$value["exchange"]);
+    Input_sina::add_day_k($value["code"],$value["exchange"]);
 }
 
 

+ 1 - 1
task/task_get_immediate_stock.php

@@ -9,7 +9,7 @@ require('../init.php');
 $codeList=Stock::getAllList();//获取股票代码
 
 foreach ($codeList as $key=>$value){
-    Input_sina_immediate::get_day_k($value["code"],$value["exchange"]);
+    Input_sina::add_to_immediate_day_k($value["code"],$value["exchange"]);
 }
 
 

+ 1 - 1
task/task_get_index_day_k.php

@@ -13,6 +13,6 @@ Input_163::getIndexDayK();
 
 //获取当天数据
 $indexList = Index::getAllList();//获取指数代码
-Input_sina_detail::getIndexDayK($indexList);
+Input_sina::getIndexDayK($indexList);
 
 ?>