|
|
|
@ -1731,3 +1731,106 @@ if (!function_exists('getDescriptionSeparate')) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取股票信息 |
|
|
|
* @param string $stockCode 股票代码 |
|
|
|
* @return array 股票数据 |
|
|
|
*/ |
|
|
|
if (!function_exists('getStockInfo')) { |
|
|
|
function getStockInfo(string $stockCode = '603933'): array |
|
|
|
{ |
|
|
|
// 默认返回数据
|
|
|
|
$defaultData = [ |
|
|
|
'code' => $stockCode, |
|
|
|
'name' => '睿能科技', |
|
|
|
'price' => '--', |
|
|
|
'change' => '--', |
|
|
|
'changePercent' => '--', |
|
|
|
'high' => '--', |
|
|
|
'low' => '--', |
|
|
|
'volume' => '--', |
|
|
|
'amount' => '--', |
|
|
|
'updateTime' => date('Y-m-d H:i:s') |
|
|
|
]; |
|
|
|
|
|
|
|
try { |
|
|
|
// 尝试从缓存获取
|
|
|
|
$cacheKey = 'stock_info_' . $stockCode; |
|
|
|
$cached = \think\facade\Cache::get($cacheKey); |
|
|
|
if (!empty($cached)) { |
|
|
|
return $cached; |
|
|
|
} |
|
|
|
|
|
|
|
// 从新浪财经获取数据
|
|
|
|
$data = fetchStockFromSina($stockCode); |
|
|
|
|
|
|
|
if ($data) { |
|
|
|
// 缓存14分50秒
|
|
|
|
\think\facade\Cache::set($cacheKey, $data, 60*15-10); |
|
|
|
return $data; |
|
|
|
} |
|
|
|
|
|
|
|
return $defaultData; |
|
|
|
} catch (\Exception $e) { |
|
|
|
return $defaultData; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从新浪财经获取股票数据 |
|
|
|
* @param string $stockCode 股票代码 |
|
|
|
* @return array|null |
|
|
|
*/ |
|
|
|
if (!function_exists('fetchStockFromSina')) { |
|
|
|
function fetchStockFromSina(string $stockCode): ?array |
|
|
|
{ |
|
|
|
$market = substr($stockCode, 0, 1) == '6' ? 'sh' : 'sz'; |
|
|
|
$url = "https://hq.sinajs.cn/list={$market}{$stockCode}"; |
|
|
|
|
|
|
|
$ch = curl_init(); |
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); |
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
|
|
'Referer: https://finance.sina.com.cn' |
|
|
|
]); |
|
|
|
|
|
|
|
$response = curl_exec($ch); |
|
|
|
curl_close($ch); |
|
|
|
|
|
|
|
if (empty($response)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// 解析新浪数据格式:var hq_str_sh603933="名称,开盘价,昨收,当前价,最高,最低,...";
|
|
|
|
if (preg_match('/="(.+)"/', $response, $matches)) { |
|
|
|
$fields = explode(',', $matches[1]); |
|
|
|
if (count($fields) >= 32) { |
|
|
|
$price = floatval($fields[3]); |
|
|
|
$prevClose = floatval($fields[2]); |
|
|
|
$change = round($price - $prevClose, 2); |
|
|
|
$changePercent = $prevClose > 0 ? round(($change / $prevClose) * 100, 2) : 0; |
|
|
|
|
|
|
|
return [ |
|
|
|
'code' => $stockCode, |
|
|
|
'name' => $fields[0], |
|
|
|
'price' => $price > 0 ? number_format($price, 2) : '--', |
|
|
|
'change' => $price > 0 ? (($change >= 0 ? '+' : '') . number_format($change, 2)) : '--', |
|
|
|
'changePercent' => $price > 0 ? (($changePercent >= 0 ? '+' : '') . number_format($changePercent, 2) . '%') : '--', |
|
|
|
'high' => floatval($fields[4]) > 0 ? number_format(floatval($fields[4]), 2) : '--', |
|
|
|
'low' => floatval($fields[5]) > 0 ? number_format(floatval($fields[5]), 2) : '--', |
|
|
|
'volume' => floatval($fields[8]) > 0 ? round(floatval($fields[8]) / 10000, 2) : '--', |
|
|
|
'amount' => floatval($fields[9]) > 0 ? round(floatval($fields[9]) / 10000, 2) : '--', |
|
|
|
'updateTime' => $fields[30] . ' ' . $fields[31] |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |