spider(); echo date('Y-m-d H:i:s').' 第'.$num.'次尝试,结果:'.$res.PHP_EOL; if ($res === false) { sleep(60); } else { break; } } } public function spider() { $url = 'https://api.bjjfnet.com/data/opencode/2032'; try { $json = file_get_contents($url); $data = json_decode($json, true); if (empty($data)) { throw new \Exception('六合彩 Empty data'); } if ($data['code'] != 0) { throw new \Exception('六合彩 code 不为 0'); } $lotteries = array_reverse($data['data']); foreach ($lotteries as $lottery) { $exists = LhcLotteryModel::where('issue', $lottery['issue'])->first(); if ($exists) { continue; } $mode = new LhcLotteryModel; $mode->issue = $lottery['issue']; $mode->open_code = $lottery['openCode']; $mode->open_time = strtotime($lottery['openTime']); $mode->save(); } } catch (\Exception $e) { Log::error("六合彩开奖数据抓取失败:".$e->getMessage()); return false; } return true; } }