Ver código fonte

指数的历史接口

wanggangtao 5 anos atrás
pai
commit
8fb62c1220

+ 2 - 13
lib/index_day_k.class.php

@@ -33,21 +33,10 @@ class Index_day_k {
         return $Table_index_day_k->getList($filter, $count, $page, $pageSize);
     }
 
-    /***
-     * @param $attrs
-     * @param int $date
-     * @return mixed
-     * @throws Exception
-     * 添加某一直股票的历史数据
-     */
-    static public function insert($attrs,$date=0)
+    static public function insert($attrs)
     {
         if (empty($attrs)) throw new Exception('参数不能为空', 102);
-        if($date===0)
-        {
-            $date=strtotime(date('Ymd'));
-        }
-        $Table_index_day_k = new Table_index_day_k($date);
+        $Table_index_day_k = new Table_index_day_k();
         $id = $Table_index_day_k->insert($attrs);
         return $id;
     }

+ 91 - 0
lib/input163Index.class.php

@@ -0,0 +1,91 @@
+<?php
+/**
+ * @author:王刚涛
+ * 用于获取当天各种指数
+ * http://hq.sinajs.cn/list=s_sz000002
+ */
+
+
+class Input163index
+{
+
+    /****
+     * @param $code
+     * @return string
+     * 返回股票指数所属的板块,在股票代码前拼接0或者1
+     */
+    static public function stock_block($code)
+    {
+        $f = substr($code, 0, 1);
+        if ($f == "6") {
+            $stock_code = "1" . $code;
+        } else {
+            $stock_code = "0" . $code;
+        }
+        return $stock_code;
+    }
+    /****
+     * @param $url
+     * @return array|bool|string
+     * 对curl_setopt进行封装,消除代码冗余
+     */
+    static private function curl_get_data($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);
+        $content1 = explode("\n", $content);//以换行符进行分割字符串
+        return $content1;
+    }
+
+    static public function get_history_index($stock_code,$start_date,$end_date)
+    {
+        $code = self::stock_block($stock_code);
+        $url = "http://quotes.money.163.com/service/chddata.html?code=" . $code . "&start=" . $start_date . "&end=" . $end_date . "&fields=TOPEN;HIGH;LOW;TCLOSE;VATURNOVER;VOTURNOVER;CHG;PCHG";
+        //构建所要发送的url
+        $content1=self::curl_get_data($url);
+
+        if (!empty($content1[1])) {
+            for ($i = count($content1) - 1; $i >0; $i--) {//排除日期,代码等标题栏
+                if (!empty($content1[$i])) {
+                    $content3 = explode(",", iconv("gbk", "utf-8", $content1[$i]));
+                }
+                else{
+                    continue;
+                }
+//                解析返回的历史数据
+                $data['date'] = strtotime($content3[0]);//用于创建表
+                $data['code'] = explode("'",$content3[1])[1];
+                $data['name'] = $content3[2];
+                $data['open_price'] = floatval($content3[3]);
+                $data['hightest_price'] = floatval($content3[4]);
+                $data['lowest_price'] = floatval($content3[5]);
+                $data['close_price'] = floatval($content3[6]);
+                $data['value'] = floatval($content3[7]);
+                $data['amount'] = floatval($content3[8]);
+                $data['increase_price'] = floatval($content3[9]);
+                $data['increase_ratio'] = floatval($content3[10]);
+                //将所要存储的数据放入一个数组中
+                $stock_name = $content3[1];
+                if ($data['close_price'] == 0) {//当收盘价为0的时候,表示此时的基于9-3点,所以不存储
+                    continue;
+                }
+                if (!empty($data)) {
+                    $msg= Index_day_k::insert($data);//将这支股票的历史数据存入数据库
+                }
+            }
+        }
+        return $msg;
+    }
+
+
+
+
+
+}
+
+?>

+ 22 - 0
lib/table/table_index_day_k.class.php

@@ -53,5 +53,27 @@ class Table_index_day_k extends Table {
         return $this->pdo->sqlinsert($this->table_fullname, $param);
     }
 
+
+    /***
+     * @param $attr
+     * @return mixed
+     * 历史指数数据
+     */
+    public function insert($attr){
+        $param = array (
+            'index_day_k_date'    => array('number', $attr['date']),
+            'index_day_k_code'    => array('string', $attr['code']),
+            'index_day_k_name'    => array('string', $attr['name']),
+            'index_day_k_open_price'    => array('number', $attr['open_price']),
+            'index_day_k_close_price'    => array('number', $attr['close_price']),
+            'index_day_k_hightest_price'    => array('number', $attr['hightest_price']),
+            'index_day_k_lowest_price'    => array('number', $attr['lowest_price']),
+            'index_day_k_increase_price'    => array('number', $attr['increase_price']),
+            'index_day_k_increase_ratio'    => array('number', $attr['increase_ratio']),
+            'index_day_k_amount'    => array('number', $attr['amount']),
+            'index_day_k_value'    => array('number', $attr['value']),        );
+        return $this->pdo->sqlinsert($this->table_fullname, $param);
+    }
+
 }
 ?>

+ 4 - 1
task/task_get_history_index.php

@@ -6,9 +6,12 @@
  * Time: 19:35
  */
 require('../init.php');
+$stock_code="399300";
+$start_date=20140101;
+$end_date=date("Ymd",time());
 $indexList=Index::getIndexList();//获取指数代码
 foreach ($indexList as $key=>$value){
-    Finance163::get_history_index_csi300($stock_code,$start_date,$end_date);
+    Input163index::get_history_index($value["code"],$start_date,$end_date);
 }