/
Ahed
/
Football-analyzer
Обзор
Документация
Войти
/
Ahed
/
Football-analyzer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/Http/Controllers/ApiSyncController.php
203 строки
6 KB
Ахед Шаабан
first_commit
18 дек 2025, 17:21
18 дек 2025, 17:21
3c7ec38
Код
Авторство
О чём код?
<?php namespace App\Http\Controllers; use App\Models\Fixture; use App\Models\League; use App\Models\Team; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class ApiSyncController extends Controller { private string $apiKey; private string $baseUrl; public function __construct() { $this->apiKey = env('FOOTBALL_API_KEY'); $this->baseUrl = 'https://v3.football.api-sports.io'; } public function fetchLeagues(){ // записывает 5 топ-лиг $topLeagues = [ 39, // Premier League (England) 140, // La Liga (Spain) 135, // Serie A (Italy) 78, // Bundesliga (Germany) 61, // Ligue 1 (France) ]; foreach ($topLeagues as $leagueId) { $response = Http::withHeaders([ 'x-apisports-key' => $this->apiKey ])->get($this->baseUrl . "/leagues", [ 'id' => $leagueId, 'season' => 2022 ]); $season = 2022; $data = $response->json(); if (!isset($data['response'][0])) { continue; } $l = $data['response'][0]; League::query()->updateOrCreate( ['api_id' => $l['league']['id']], [ 'name' => $l['league']['name'], 'country' => $l['country']['name'], 'season' => $season, ] ); } return response()->json([ 'status' => 'success', 'message' => 'Top 5 leagues loaded successfully.' ]); } public function fetchTeams(Request $request){ // записывает комнады из конкретной лиги $leagueId = $request->input('league_id'); // league_id приходит от пользователя $season = $request->input('season', 2022); // по умолчанию — текущий сезон if (!$leagueId) { return response()->json(['error' => 'league_id required'], 400); } // $url = "https://api-football-v1.p.rapidapi.com/v3/teams?league={$leagueId}&season={$season}"; // // $response = Http::withHeaders([ // 'x-rapidapi-key' => env('FOOTBALL_API_KEY'), // 'x-rapidapi-host' => 'api-football-v1.p.rapidapi.com' // ])->get($url); $response = Http::withHeaders([ 'x-apisports-key' => env('FOOTBALL_API_KEY') ])->get('https://v3.football.api-sports.io/teams', [ 'league' => $leagueId, 'season' => $season ]); if ($response->failed()) { return response()->json(['error' => 'Failed to fetch teams'], 500); } $data = $response->json(); if (!isset($data['response'])) { return response()->json(['error' => 'Invalid API response'], 500); } $league = League::query()->where('api_id','=',$leagueId)->firstOrFail(); foreach ($data['response'] as $item) { $team = $item['team'] ?? null; if (!$team) { continue; } Team::query()->updateOrCreate( ['api_id' => $team['id']], [ 'league_id' => $league->id, 'name' => $team['name'], ] ); } return response()->json(['status' => 'Teams updated']); } public function fetchFixtures(Request $request){ // записывает матчи конкретной команды из конкретной лиги $leagueId = $request->input('league_id'); // league_id приходит от пользователя $teamId = $request->input('team_id'); $season = $request->input('season', 2022); // по умолчанию — текущий сезон $league = League::query()->where('api_id','=',$leagueId)->firstOrFail(); $team = Team::query()->where('api_id', '=', $teamId)->firstOrFail(); if (!$leagueId) { return response()->json(['error' => 'league_id required'], 400); } if (!$teamId) { return response()->json(['error' => 'team_id required'], 400); } // $url = "https://api-football-v1.p.rapidapi.com/v3/fixtures?league={$leagueId}&team={$teamId}&season={$season}"; // // $response = Http::withHeaders([ // 'x-rapidapi-key' => env('FOOTBALL_API_KEY'), // 'x-rapidapi-host' => 'api-football-v1.p.rapidapi.com' // ])->get($url); $response = Http::withHeaders([ 'x-apisports-key' => env('FOOTBALL_API_KEY') ])->get('https://v3.football.api-sports.io/fixtures', [ 'league' => $leagueId, 'team' => $teamId, 'season' => $season ]); if ($response->failed()) { return response()->json(['error' => 'Failed to fetch teams'], 500); } $data = $response->json(); if (!isset($data['response'])) { return response()->json(['error' => 'Invalid API response'], 500); } foreach ($data['response'] as $item) { $fixture = $item['fixture'] ?? null; if (!$fixture) { continue; } $isHome = $item['teams']['home']['id'] === $team->api_id; $goalsFor = $isHome ? $item['goals']['home'] : $item['goals']['away']; $goalsAgainst = $isHome ? $item['goals']['away'] : $item['goals']['home']; if ($goalsFor > $goalsAgainst) { $result = 'win'; } elseif ($goalsFor < $goalsAgainst) { $result = 'loss'; } else { $result = 'draw'; } Fixture::query()->updateOrCreate( ['api_id' => $fixture['id']], [ 'league_id' => $league->id, 'team_id' => $team->id, 'date' => $fixture['date'], 'goals_for' => $goalsFor, 'goals_against' => $goalsAgainst, 'result' => $result, 'season' => $season, ] ); } return response()->json(['status' => 'fixtures updated']); } }