ソースを参照

“xig

修改bug
chenbo 4 年 前
コミット
f484164602
共有4 個のファイルを変更した83 個の追加4 個の削除を含む
  1. 80 0
      lib/input_sina_detail.class.php
  2. 0 2
      lib/input_sina_simple.class.php
  3. 2 1
      task/task_get_index_day_k.php
  4. 1 1
      task/task_get_index_day_k.sh

+ 80 - 0
lib/input_sina_detail.class.php

@@ -0,0 +1,80 @@
+<?php
+/**
+ * @author:王刚涛
+ * 用于获取 当天的股票数据并存入数据库,通过$url,可以设置需要获取的时间段
+ * http://hq.sinajs.cn/list=s_sz000002
+ */
+
+
+class Input_sina_detail
+{
+    const GLOBAL_URL = "http://hq.sinajs.cn/list=";
+
+    static private function curl_get_data($url, $codeList){           //一次批量获取股票列表
+        if (empty($codeList)) {
+            return 0;
+        }
+        $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);//防止中文乱码
+        $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;
+    }
+
+    //获取index_day_k
+    static public function getIndexDayK($indexList)
+    {
+        $codeList = array();
+        $codeIndex = 0;
+        $url = self::GLOBAL_URL;
+        foreach ($indexList as $item) {
+            $codeStr = sprintf('%06s', $item['code']);
+            $url .= $item['exchange'].$codeStr.',';
+            $codeList[$codeIndex] = $codeStr;
+            $codeIndex++;
+        }
+        $contentList = self::curl_get_data($url, $codeList);
+        foreach ($contentList as $key => $item) {
+            $data['code'] = $item['code'];
+            $data['date'] = 0;
+            $data['increase_ratio'] = 0; //此接口中没有涨跌幅,设为0
+            $data['name'] = trim($item[0]);
+            $data['increase_price'] = floatval($item[3] - $item[2]);//涨跌
+            $data['timestamp'] = 0;//当天的0点时间戳
+            $data['open_price'] = floatval($item[1]);//开盘价
+            $data['close_price'] = floatval($item[3]);//3点之后的当前价格就是收盘价
+            $data['highest_price'] = floatval($item[4]);//今日最高价
+            $data['lowest_price'] = floatval($item[5]);//今日最低价
+            $data['amount'] =  (int)($item[8]);//成交的股票数,以百为单位
+            $data['value'] = (int)($item[9]);//成交金额以万为单位
+            if (!empty($item[0])) {                            //判断非法股票代码
+                Index_day_k::addOrUpdateByCodeDate($data);
+            }
+        }
+        return;
+    }
+
+}
+
+?>

+ 0 - 2
lib/input_sina_simple.class.php

@@ -165,8 +165,6 @@ class Input_sina_simple
         $contentList = self::curl_get_data($url, $codeList);
         foreach ($contentList as $key => $item) {
             $data['code'] = $item['code'];
-            $data['name'] = trim($item[0]);
-            $data['date'] = date('Ymd',time());
             $data['increase_ratio'] = floatval(trim($item[3]));
             if (!empty($item[0])) {                            //判断非法股票代码
                 Index_day_k::addOrUpdateByCodeDate($data);

+ 2 - 1
task/task_get_index_day_k.php

@@ -9,6 +9,7 @@
 require('../init.php');
 
 $indexList = Index::getAllList();//获取指数代码
-Input_sina_simple::getIndexDayKIncreaseRatio($indexList);
+Input_sina_detail::getIndexDayK($indexList);
+//Input_sina_simple::getIndexDayKIncreaseRatio($indexList);
 
 ?>

+ 1 - 1
task/task_get_index_day_k.sh

@@ -7,6 +7,6 @@ echo '正在获取指数日K数据...'
 nohup php ./task_get_index_day_k.php &
 sleep 5s
 echo "完成!"
-f
+
 sleep 1s