php定时执行mysql脚本_php定时执行请求,并存入数据库

//数据请求

//$t1 = time();

$i=0;

do{

$url = '';

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

$res = json_decode($file_contents,true);

$issue = $res[0]['issue'];

if( !$issue ){

echo "api wrong";

$i++;

sleep(10);

continue;

}

$opendate = $res[0]['opendate'];

$code = $res[0]['code'];

$code = explode(',',$code);

$lotterycode = $res[0]['lotterycode'];

$officialissue = $res[0]['officialissue'];

$servername = "127.0.0.1";

$username = "root";

$password = "5c28bc46a05afd59";

// $password = "root";

$dbname = "onebase";

try {

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

// 设置 PDO 错误模式,用于抛出异常

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql_querry = "select * from ob_code where issue = '".$issue."'";

$res= $conn->query($sql_querry);

if($res->rowCount()!=0){

}else{

$sql = "insert into ob_code (issue,opendate,code0,code1,code2,code3,code4,lotterycode,officialissue) values ('$issue','$opendate','$code[0]','$code[1]','$code[2]','$code[3]','$code[4]','$lotterycode','$officialissue')";

// 使用 exec() ,没有结果返回

$conn->exec($sql);

}

}

catch(PDOException $e)

{

echo "
" . $e->getMessage();

}

$conn = null;

$i++;

sleep(10);

}

while($i<=4);

//$t2 = time();

//echo ($t2-$t1);

配合crontab 执行定时任务 可以实现每10秒执行一次此任务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值