【发布时间】:2018-10-29 15:06:53
【问题描述】:
我的应用程序:我的应用程序允许用户预测下一个比赛日所有即将举行的足球比赛的比分。我从 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);
}
getMatchesjob(这个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);
}
【问题讨论】:
标签: php laravel laravel-jobs