$item) { $data['code'] = $item['code']; $data['date'] = ConverseDate($item[30]); $data['name'] = trim($item[0]); $data['increase_price'] = floatval($item[3] - $item[2]); //涨跌价 $data['timestamp'] = strtotime($item[30]); //当天的0点时间戳 $data['open_price'] = floatval($item[1]); //开盘价 $data['close_price'] = floatval($item[3]); //当前价,15点之后就是收盘价 $data['highest_price'] = floatval($item[4]); //最高价 $data['lowest_price'] = floatval($item[5]); //最低价 $data['amount'] = self::getAmountForIndexDayK($data['code'], (int)($item[8])); //成交量,以手为单位 $data['value'] = (int)($item[9]); //成交金额,以万为单位 $data['increase_ratio'] = 0; //涨跌幅 if (!empty($data['open_price'])) $data['increase_ratio'] = round($data['increase_price'] * 100 / $data['open_price'], 2); if (!empty($item[0])) { //判断非法股票代码 Index_day_k::addOrUpdateByCodeDate($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; } //获取指定条件的股票代码和名称列表 static private function getPartStockCodeAndNameList($exchange, $sector, $start, $end) { $data['exchange'] = $exchange; $data['sector'] = $sector; $codeList = array(); $codeIndex = 0; $url = self::INPUT_SINA_URL; for($code = $start;$code < $end; $code++){ $codeStr = sprintf('%06s', $code); $url .= 's_'.$exchange.$codeStr.','; $codeList[$codeIndex] = $codeStr; $codeIndex++; } $contentList = self::_render_index_k_day_curl_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 private function _render_index_k_day_curl_data($url,$indexList) { $content = self::curl_exec($url); $codeList = array(); $codeIndex = 0; foreach ($indexList as $item) { $codeStr = sprintf('%06s', $item['code']); $url .= $item['exchange'].$codeStr.','; $codeList[$codeIndex] = $codeStr; $codeIndex++; } if (empty($codeList)) { return 0; } $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 getAmountForIndexDayK($code, $amount) { $newAmount = $amount; $index = Index::getInfoByCode($code); if ($index['exchange'] == STOCK_EXCHANGE_SZ) { $newAmount = round($amount / 100); } return $newAmount; } /**** * @param $url * @return array|bool|string * 对curl_setopt进行封装,消除代码冗余 */ static private function curl_exec($url) { $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);//防止中文乱码 return $content; } }