Explorar el Código

获取所有股票列表

chenbo hace 5 años
padre
commit
44610cf925
Se han modificado 4 ficheros con 109 adiciones y 41 borrados
  1. 11 0
      data/get_stock_all_code_name_by_sina.php
  2. 12 10
      lib/sina.class.php
  3. 43 29
      lib/stock.class.php
  4. 43 2
      lib/table/table_stock.class.php

+ 11 - 0
data/get_stock_all_code_name_by_sina.php

@@ -0,0 +1,11 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: 陈波
+ * Date: 2020/11/24
+ * Time: 19:16
+ * 获取所有个股的Code和Name,入库
+ */
+require('../init.php');
+
+sina::getAllStockCodeAndName();

+ 12 - 10
lib/sina.class.php

@@ -6,14 +6,14 @@
 error_reporting(E_ALL & ~E_NOTICE);
 
 
-class sina
+class Sina
 {
     /****
      * @param $url
      * @return array|bool|string
      * 对curl_setopt进行封装,消除代码冗余
      */
-      static public function curl_get_data($url){
+      static private function curl_get_data($url){
         $curlHandle = curl_init();
         curl_setopt($curlHandle, CURLOPT_URL, $url);
         curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
@@ -23,21 +23,23 @@ class sina
         $content = curl_exec($curlHandle);
         curl_close($curlHandle);//防止中文乱码
         $content2 = iconv("gbk", "utf-8", $content);//子串
-        $content3 = substr($content2, strripos($content2, "=") + 1);
+        $content3 = substr($content2, strripos($content2, "=\"") + 2); //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510,  ="之后部分
         $content3 = explode(",", $content3);//分割
         return $content3;
     }
 
-
-//市场上所有的股票 boniu_stock
-     static public function get_boniu_stock($code)
+    //获取全部股票代码和名称列表
+    static public function getAllStockCodeAndName()
     {
-
+        $url = "http://hq.sinajs.cn/list=s_sz000002";
+        $content = self::curl_get_data($url);
+        $data['code'] = '000002';
+        $data['name'] = trim($content[0]);
+        $data['exchange'] = 'sz';
+        $data['sector'] = 20;
+        return Stock::updateCodeAndName($data);
     }
 
-
-
-
     /****
      * @param string $code
      * @return mixed

+ 43 - 29
lib/stock.class.php

@@ -8,36 +8,50 @@
 
 class Stock {
 
-	/**
-	 * 记录管理员日志
-	 * 
-	 * @param $log       日志内容
-	 * 
-	 */
-	static public function add($log){
-
-	}
-    
-	/** 
-	 * 管理员日志记录列表
-	 * 
-	 * @param $page        当前页
-	 * @param $pagesize    每页大小
-	 * 
-	 */
-	static public function getListByPage($page, $pagesize){
-		$filter = array();
+    public function __construct()
+    {
+
+    }
+
+    static public function getInfoById($id)
+    {
+        $Table_stock = new Table_stock();
+        return $Table_stock->getInfoById($id);
+    }
+
+
+    static public function add($attrs)
+    {
+        if (empty($attrs)) throw new Exception('参数不能为空', 101);
+
+        $Table_stock = new Table_stock();
+        $id = $Table_stock->add($attrs);
+
+        return $id;
+    }
+
+    static public function getList($filter = array(), $count=0, $page=0, $pageSize=0)
+    {
+        $Table_stock = new Table_stock();
+        return $Table_stock->getList($filter, $count, $page, $pageSize);
+    }
+
+    static public function updateCodeAndName($attrs)
+    {
+        if (empty($attrs)) throw new Exception('参数不能为空', 101);
 
         $Table_stock = new Table_stock();
-		return $Table_stock->getList($filter, 0, $page, $pagesize);
-	}
-    
-	//管理员日志总数
-	static public function getCountAll(){
-		$filter = array();
-
-        $Table_stock = new Table_st_log();
-		return $Table_stock->getList($filter, 1);
-	}
+        $rs = $Table_stock->getInfoByCode($attrs['code']);
+        if (empty($rs)) {
+            $id = $Table_stock->addCodeAndName($attrs);
+        } else {
+            $id = $rs['id'];
+            $Table_stock->update($id, $attrs);
+        }
+
+        return $id;
+    }
+
 }
+
 ?>

+ 43 - 2
lib/table/table_stock.class.php

@@ -43,7 +43,7 @@ class Table_stock extends Table {
             'stock_desc'            => array('string', $attr['desc']),
             'stock_tradable_amount' => array('number', $attr['tradable_amount']),
             'stock_tradable_value'  => array('number', $attr['tradable_value']),
-            'stock_ total _amount'  => array('number', $attr['total_amount']),
+            'stock_total_amount'    => array('number', $attr['total_amount']),
             'stock_total_value'     => array('number', $attr['total_value']),
             'stock_profitable'      => array('number', $attr['profitable']),
             'stock_pb'              => array('number', $attr['pb']),
@@ -57,6 +57,47 @@ class Table_stock extends Table {
         return $this->pdo->sqlinsert($this->table_fullname, $param);
     }
 
-    
+    public function addCodeAndName($attr){
+        $param = array (
+            'stock_code'            => array('string', $attr['code']),
+            'stock_name'            => array('string', $attr['name']),
+            'stock_desc'            => array('string', ''),
+            'stock_tradable_amount' => array('number', 0),
+            'stock_tradable_value'  => array('number', 0),
+            'stock_total_amount'    => array('number', 0),
+            'stock_total_value'     => array('number', 0),
+            'stock_profitable'      => array('number', 0),
+            'stock_pb'              => array('number', 0),
+            'stock_pe_static'       => array('number', 0),
+            'stock_pe_dynamic'      => array('number', 0),
+            'stock_pe_ttm'          => array('number', 0),
+            'stock_exchange'        => array('string', $attr['exchange']),
+            'stock_sector'          => array('number', $attr['sector']),
+        );
+
+        return $this->pdo->sqlinsert($this->table_fullname, $param);
+    }
+
+    public function getInfoByCode($code){
+
+        //查询语句必须用sql_check_input检查参数
+        $code = trim($code);
+        $code = $this->pdo->sql_check_input(array('string', $code));
+
+        $sql = "select * from ". $this->table_fullname ." where stock_code = $code limit 1";
+
+        $rs = $this->pdo->sqlQuery($sql);
+        $r  = array();
+        if($rs){
+            foreach($rs as $key => $val){
+                $r[$key] = $this->dataToAttr($val);
+            }
+            return $r[0];
+        }else{
+            return $r;
+        }
+    }
+
+
 }
 ?>