input_sina_simple.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @author:王刚涛
  4. * 用于获取 当天的股票数据并存入数据库,通过$url,可以设置需要获取的时间段
  5. * http://hq.sinajs.cn/list=s_sz000002
  6. */
  7. class Input_sina_simple
  8. {
  9. const GLOBAL_URL = "http://hq.sinajs.cn/list=";
  10. static private function curl_get_data($url, $codeList){ //一次批量获取股票列表
  11. if (empty($codeList)) {
  12. return 0;
  13. }
  14. $curlHandle = curl_init();
  15. curl_setopt($curlHandle, CURLOPT_URL, $url);
  16. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  17. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  18. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
  19. curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
  20. $content = curl_exec($curlHandle);
  21. curl_close($curlHandle);//防止中文乱码
  22. $content2 = iconv("gbk", "utf-8", $content);//子串
  23. $arr1 = explode(";", $content2);//按股票分割
  24. $arr2 = array();
  25. $index = 0;
  26. $codeIndex = 0;
  27. foreach ($arr1 as $key => $item) {
  28. $tempItem = substr($item, stripos($item, "=\"") + 2); //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510, ="之后部分
  29. $tempItem = substr($tempItem, 0, strripos($tempItem, "\"")); //获取 万 科A,30.94,0.14,0.45,584039,178827"; ";之前部分
  30. $tempItem = trim($tempItem);
  31. if (!empty($tempItem)) {
  32. $arr2[$index] = explode(",", $tempItem);//分割
  33. $arr2[$index]['code'] = $codeList[$codeIndex];
  34. $index++;
  35. }
  36. $codeIndex++;
  37. }
  38. return $arr2;
  39. }
  40. //获取指定条件的股票代码和名称列表
  41. static private function getPartStockCodeAndNameList($exchange, $sector, $start, $end)
  42. {
  43. $data['exchange'] = $exchange;
  44. $data['sector'] = $sector;
  45. $codeList = array();
  46. $codeIndex = 0;
  47. $url = self::GLOBAL_URL;
  48. for($code = $start;$code < $end; $code++){
  49. $codeStr = sprintf('%06s', $code);
  50. $url .= 's_'.$exchange.$codeStr.',';
  51. $codeList[$codeIndex] = $codeStr;
  52. $codeIndex++;
  53. }
  54. $contentList = self::curl_get_data($url, $codeList);
  55. foreach ($contentList as $key => $item) {
  56. $data['code'] = $item['code'];
  57. $data['name'] = trim($item[0]);
  58. if (!empty($item[0])) { //判断非法股票代码
  59. Stock::addOrUpdateByCode($data);
  60. }
  61. }
  62. return;
  63. }
  64. //获取全部股票代码和名称列表
  65. static public function getAllStockCodeAndNameList()
  66. {
  67. //上证主板 60****
  68. for($i = 0; $i < 50; $i++) {
  69. $start = 600000 + $i * 200;
  70. self::getPartStockCodeAndNameList('sh', 10, $start, $start + 200);
  71. }
  72. //上证科创板 688***
  73. for($i = 0; $i < 5; $i++) {
  74. $start = 688000 + $i * 200;
  75. self::getPartStockCodeAndNameList('sh', 11, $start, $start + 200);
  76. }
  77. //深证主板 000000~001999
  78. for($i = 0; $i < 10; $i++) {
  79. $start = 0 + $i * 200;
  80. self::getPartStockCodeAndNameList('sz', 20, $start, $start + 200);
  81. }
  82. //深证中小板 002000~002999
  83. for($i = 0; $i < 5; $i++) {
  84. $start = 2000 + $i * 200;
  85. self::getPartStockCodeAndNameList('sz', 21, $start, $start + 200);
  86. }
  87. //深证创业板 300***
  88. for($i = 0; $i < 5; $i++) {
  89. $start = 300000 + $i * 200;
  90. self::getPartStockCodeAndNameList('sz', 22, $start, $start + 200);
  91. }
  92. return;
  93. }
  94. //获取指定条件的股票代码和名称列表
  95. static public function getAllIndexCodeAndNameList()
  96. {
  97. $i = 0;
  98. $index[$i]['code'] = '000001';
  99. $index[$i]['name'] = '上证指数';
  100. $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
  101. $index[$i]['sector'] = STOCK_SECTOR_SH_MAIN;
  102. $i++;
  103. $index[$i]['code'] = '399001';
  104. $index[$i]['name'] = '深证成指';
  105. $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
  106. $index[$i]['sector'] = STOCK_SECTOR_SZ_MAIN;
  107. $i++;
  108. $index[$i]['code'] = '000016';
  109. $index[$i]['name'] = '上证50';
  110. $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
  111. $index[$i]['sector'] = STOCK_SECTOR_SH_MAIN;
  112. $i++;
  113. $index[$i]['code'] = '399300';
  114. $index[$i]['name'] = '沪深300';
  115. $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
  116. $index[$i]['sector'] = STOCK_SECTOR_SZ_MAIN;
  117. $i++;
  118. $index[$i]['code'] = '399006';
  119. $index[$i]['name'] = '创业板指';
  120. $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
  121. $index[$i]['sector'] = STOCK_SECTOR_SZ_CHUANGYE;
  122. $i++;
  123. $index[$i]['code'] = '000688';
  124. $index[$i]['name'] = '科创50';
  125. $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
  126. $index[$i]['sector'] = STOCK_SECTOR_SH_KECHUANG;
  127. foreach ($index as $key => $item) {
  128. if (!empty($item['code'])) { //判断非法股票代码
  129. Index::addOrUpdateByCode($item);
  130. }
  131. }
  132. return;
  133. }
  134. }
  135. ?>