我的应用程序:我的应用程序允许用户预测下一个比赛日所有即将到来的足球比赛的比分。我从 API 获取这些数据并将即将推出的游戏存储在我的数据库中。这些即将到来的游戏有一个status
。Scheduled
现在我想每隔几分钟运行一次 cronjob 来检查这些匹配项的状态是否已更改为in_play
或finished
,如果是这种情况,我想status field
在我的数据库中更新我的,以正确匹配来自 api 的状态字段。
如何检查状态是否已更改并在我的数据库中修改正确的匹配项?我有一个match_id
可以用于此的存储?
我的代码:
updateStatus
工作
public function handle()
{
$this->updateStatus();
}
public function updateStatus() {
$this->getMatches();
// check if status has been changed from schedulded to 'in_play' or 'finished'
// update the match status of the right matches in my database
}
public function getMatches() {
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
getMatches
job(这个job获取api数据并存入数据库)
public function handle()
{
$this->saveMatches();
}
public function saveMatches()
{
$matches = $this->getMatches();
collect($matches['matches'])
->each(function ($match, $key) {
$date = new DateTime($match['utcDate']);
Match::create([
'match_id' => $match['id'],
'homeTeam' => $match['homeTeam']['name'],
'awayTeam' => $match['awayTeam']['name'],
'status' => $match['status'],
'date' => $date->format('Y-m-d'),
'time' => $date->format('H:i'),
'matchday' => $match['matchday'],
'homeScore'=> $match['score']['fullTime']['homeTeam'],
'awayScore'=> $match['score']['fullTime']['awayTeam']
]);
});
}
public function getMatches()
{
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}