<?php
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间戳
$current_time = time();
function zz($preg,$con,$num=1){
preg_match($preg,$con,$arr);
return $arr[$num];
}
// 读取上次写入时间
$previous_time_file = 'last_time.txt';
if (file_exists($previous_time_file)) {
$previous_time = intval(file_get_contents($previous_time_file));
} else {
// 如果不存在,则初始化为当前时间,以便第一次检查总是可以通过
$previous_time = $current_time;
}
// 计算时间差是否超过1小时
$one_hour = 60; // 秒数
$time_difference = $current_time - $previous_time;
echo $time_difference;
echo "<br>";
$currentDateTime = new DateTime();
$tradingStartTime = new DateTime('today 09:30:00');
$tradingEndTime = new DateTime('today 15:00:00');
// 判断是否在交易时间内
if ($currentDateTime >= $tradingStartTime && $currentDateTime <= $tradingEndTime) {
if ($time_difference >= $one_hour) {
// 在时间内执行的操作
executeTradingFunction();
file_put_contents($previous_time_file, $current_time);
}
} else {
// 不在时间内,不执行任何操作或者执行其他非交易时段的任务
echo "当前时间不在时间内,无法执行相关操作。";
}
function executeTradingFunction() {
// 这里是你的交易相关的代码
/////////////////////////////////////////////////////////提取数据//////////////////////////////////////
$cookie_file = dirname(__FILE__).'/cookie.txt';
$url = "https://xueqiu.com/";
$ch = curl_init($url); //初始化
curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //存储cookies
curl_exec($ch);
curl_close($ch);
$url="https://xueqiu.com/S/SZ159509";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies
$response = curl_exec($ch);
curl_close($ch);
//$content=json_decode($response, true);
$con= zz('#window.STOCK_PAGE(.*)currentUser#iUs',$response);
//echo $con;
$shijia= zz('#市价:<span>(.*)</span>#iUs',$con);
$jingzhi= zz('#单位净值:<span>(.*)</span>#iUs',$con);
$yijialv= zz('#溢价率:<span>(.*)%</span>#iUs',$con);
$chengjiaoe= zz('#"amount":(.*),"#iUs',$con); //成交额
$fene= zz('#"total_shares":(.*),"#iUs',$con); //总份额
$shizhi= zz('#"market_capital":(.*),"#iUs',$con); //总市值
$hs=round(($chengjiaoe*100/$shizhi),2);
$statur= zz('#"status":"(.*)",#iUs',$con);
echo $statur;
//if($statur!="已收盘" || $statur!="休市" || $statur!="未开盘"){
$newData = $fene.";".$yijialv.";".$jingzhi.";".$shijia.";".$hs.";".$chengjiaoe;
// 将新数据写入临时txt文件(用于比较)
$tempFile = 'temp_data.txt';
file_put_contents($tempFile, $newData);
// 读取暂存的txt文件内容
$previousDataFile = 'stored_data.txt';
$previousData = file_get_contents($previousDataFile);
if ($newData !== $previousData) {
require_once('Log.class.php');
$filename = "logs/159509.txt";
$msg = array(
'sj' => $shijia,
'jz' => $jingzhi,
'yjl' => $yijialv,
'hs' => $hs,
'fe' => $fene,
'cje' => $chengjiaoe
);
$Log = new Log();
$Log->writeLog($filename,$msg);
echo "ok";
// 更新上次缓存内容
copy($tempFile, $previousDataFile);
}
// 更新上次执行时间
}//操作大函数的结尾
?>
获取数据信息