input_sina_simple.class.php 5.1 KB

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