index_gem.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Class Index_gem
  4. * * @author 王刚涛
  5. * 日线数据
  6. */
  7. class Index_gem {
  8. public function __construct()
  9. {
  10. }
  11. static public function getInfoById($id)
  12. {
  13. $Table_index_gem = new Table_index_gem();
  14. return $Table_index_gem->getInfoById($id);
  15. }
  16. static public function insert($attrs)
  17. {
  18. if (empty($attrs)) throw new Exception('参数不能为空', 102);
  19. $Table_index_gem = new Table_index_gem();
  20. $id = $Table_index_gem->insert($attrs);
  21. return $id;
  22. }
  23. static public function get_history_index_gem($stock_code,$start_date,$end_date)
  24. {
  25. $code = self::stock_block($stock_code);
  26. $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";
  27. //构建所要发送的url
  28. $curlHandle = curl_init();
  29. curl_setopt($curlHandle, CURLOPT_URL, $url);
  30. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  32. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
  33. curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
  34. $content = curl_exec($curlHandle);
  35. curl_close($curlHandle);
  36. $content1 = explode("\n", $content);//以换行符进行分割字符串
  37. if (!empty($content1[1])) {
  38. for ($i = count($content1) - 1; $i >0; $i--) {//排除日期,代码等标题栏
  39. if (!empty($content1[$i])) {
  40. $content3 = explode(",", iconv("gbk", "utf-8", $content1[$i]));
  41. }
  42. else{
  43. continue;
  44. }
  45. var_dump($content3);
  46. // 解析返回的历史数据
  47. $t = explode("-",$content3[0]);
  48. $date = trim($t[0].$t[1]);
  49. $time = trim($t[0].$t[1].$t[2]);
  50. $data['date'] = $date;//用于创建表
  51. $data['time'] = $time;//用于获取时间戳
  52. $data['code'] = intval(explode("'",$content3[1])[1]);
  53. $data['open_price'] = floatval($content3[3]);
  54. $data['hightest_price'] = floatval($content3[4]);
  55. $data['lowest_price'] = floatval($content3[5]);
  56. $data['close_price'] = floatval($content3[6]);
  57. $data['value'] = floatval($content3[7]);
  58. $data['amount'] = floatval($content3[8]);
  59. $data['increase_price'] = floatval($content3[9]);
  60. $data['increase_value'] = floatval($content3[10]);
  61. //将所要存储的数据放入一个数组中
  62. $stock_name = $content3[1];
  63. if ($data['close_price'] == 0) {//当收盘价为0的时候,表示此时的基于9-3点,所以不存储
  64. continue;
  65. }
  66. if (!empty($data)) {
  67. $msg= Index_csi300::insert($data);//将这支股票的历史数据存入数据库
  68. }
  69. }
  70. }
  71. return $msg;
  72. }
  73. /***
  74. * @param $log
  75. * @return mixed
  76. * @throws Exception
  77. *
  78. */
  79. static public function add($attrs){
  80. if (empty($attrs)) throw new Exception('参数不能为空', 102);
  81. $Table_index_gem = new Table_index_gem();
  82. $id = $Table_index_gem->add($attrs);
  83. return $id;
  84. }
  85. static public function getList($filter = array(), $count=0, $page=0, $pageSize=0)
  86. {
  87. $Table_index_gem = new Table_index_gem();
  88. return $Table_index_gem->getList($filter, $count, $page, $pageSize);
  89. }
  90. }
  91. ?>