| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- /**
- * @author:王刚涛
- * 用于获取 当天的股票数据并存入数据库,通过$url,可以设置需要获取的时间段
- * http://hq.sinajs.cn/list=s_sz000002
- */
- class Input_sina_simple
- {
- const GLOBAL_URL = "http://hq.sinajs.cn/list=";
- static private function curl_get_data($url, $codeList){ //一次批量获取股票列表
- if (empty($codeList)) {
- return 0;
- }
- $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);//防止中文乱码
- $content2 = iconv("gbk", "utf-8", $content);//子串
- $arr1 = explode(";", $content2);//按股票分割
- $arr2 = array();
- $index = 0;
- $codeIndex = 0;
- foreach ($arr1 as $key => $item) {
- $tempItem = substr($item, stripos($item, "=\"") + 2); //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510, ="之后部分
- $tempItem = substr($tempItem, 0, strripos($tempItem, "\"")); //获取 万 科A,30.94,0.14,0.45,584039,178827"; ";之前部分
- $tempItem = trim($tempItem);
- if (!empty($tempItem)) {
- $arr2[$index] = explode(",", $tempItem);//分割
- $arr2[$index]['code'] = $codeList[$codeIndex];
- $index++;
- }
- $codeIndex++;
- }
- return $arr2;
- }
- //获取指定条件的股票代码和名称列表
- static private function getPartStockCodeAndNameList($exchange, $sector, $start, $end)
- {
- $data['exchange'] = $exchange;
- $data['sector'] = $sector;
- $codeList = array();
- $codeIndex = 0;
- $url = self::GLOBAL_URL;
- for($code = $start;$code < $end; $code++){
- $codeStr = sprintf('%06s', $code);
- $url .= 's_'.$exchange.$codeStr.',';
- $codeList[$codeIndex] = $codeStr;
- $codeIndex++;
- }
- $contentList = self::curl_get_data($url, $codeList);
- foreach ($contentList as $key => $item) {
- $data['code'] = $item['code'];
- $data['name'] = trim($item[0]);
- if (!empty($item[0])) { //判断非法股票代码
- Stock::addOrUpdateByCode($data);
- }
- }
- return;
- }
- //获取全部股票代码和名称列表
- static public function getAllStockCodeAndNameList()
- {
- //上证主板 60****
- for($i = 0; $i < 50; $i++) {
- $start = 600000 + $i * 200;
- self::getPartStockCodeAndNameList('sh', 10, $start, $start + 200);
- }
- //上证科创板 688***
- for($i = 0; $i < 5; $i++) {
- $start = 688000 + $i * 200;
- self::getPartStockCodeAndNameList('sh', 11, $start, $start + 200);
- }
- //深证主板 000000~001999
- for($i = 0; $i < 10; $i++) {
- $start = 0 + $i * 200;
- self::getPartStockCodeAndNameList('sz', 20, $start, $start + 200);
- }
- //深证中小板 002000~002999
- for($i = 0; $i < 5; $i++) {
- $start = 2000 + $i * 200;
- self::getPartStockCodeAndNameList('sz', 21, $start, $start + 200);
- }
- //深证创业板 300***
- for($i = 0; $i < 5; $i++) {
- $start = 300000 + $i * 200;
- self::getPartStockCodeAndNameList('sz', 22, $start, $start + 200);
- }
- return;
- }
- //获取指定条件的股票代码和名称列表
- static public function getAllIndexCodeAndNameList()
- {
- $i = 0;
- $index[$i]['code'] = '000001';
- $index[$i]['name'] = '上证指数';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
- $index[$i]['sector'] = STOCK_SECTOR_SH_MAIN;
- $i++;
- $index[$i]['code'] = '399001';
- $index[$i]['name'] = '深证成指';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
- $index[$i]['sector'] = STOCK_SECTOR_SZ_MAIN;
- $i++;
- $index[$i]['code'] = '000016';
- $index[$i]['name'] = '上证50';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
- $index[$i]['sector'] = STOCK_SECTOR_SH_MAIN;
- $i++;
- $index[$i]['code'] = '399300';
- $index[$i]['name'] = '沪深300';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
- $index[$i]['sector'] = STOCK_SECTOR_SZ_MAIN;
- $i++;
- $index[$i]['code'] = '399006';
- $index[$i]['name'] = '创业板指';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SZ;
- $index[$i]['sector'] = STOCK_SECTOR_SZ_CHUANGYE;
- $i++;
- $index[$i]['code'] = '000688';
- $index[$i]['name'] = '科创50';
- $index[$i]['exchange'] = STOCK_EXCHANGE_SH;
- $index[$i]['sector'] = STOCK_SECTOR_SH_KECHUANG;
- foreach ($index as $key => $item) {
- if (!empty($item['code'])) { //判断非法股票代码
- Index::addOrUpdateByCode($item);
- }
- }
- return;
- }
- }
- ?>
|