Creating a Laravel application to obtain fixture information for data entry.
Learning laravel was a big step for me, I had never used a framework before, and I was not sure what to expect. I knew that Pitchero used this framework and I had friends who had used it in the past but for myself who had simple used procedural PHP this was a big step.
I started with Laracasts, the best location for learning laravel and how it works, by following the tutorials I was able to get a basic understanding of how the framework worked and how to use it. I was able to create a basic CRUD application.
I find I learn best when I need to solve a problem, so I decided to create a Laravel application to obtain fixture information from the FA Full-Time system. This was a system that I had used for a few years and I knew that it was not the best system to use. I knew they had an API that was undocumented and I wanted to see if I could use it to obtain fixture information.
The first task was to understand how to obtain the information from the APIs, this required me to inspect HTTP requests, understand what data was passed to the API and how I could utilise this data.
Viewing Leagues
Viewing the leagues is the most basic of pages returned by this application. It simply get the Data from the database and outputs it into a simple blade view.
class ListLeaguesController extends Controller
{
public function index()
{
$leagues = DB::table('fa_leagues')
->whereNotNull('season_id_2020')->get();
return view(
'frontend.football_leagues_list',
[
'leagues' => $leagues,
]
);
}
}
Getting the divisions
To get the divisions I needed to understand the Full-Time APIs, Notably, the Full-Time system predominantly utilizes POST requests for its endpoints rather than GET requests. By inspecting their requests via development tools offered in Google Chrome I was able to understand what they required and how to use them externally from their application.
In the following code, it might not be immediately evident that the divisions are being retrieved from a league due to poor use of variable names.
public function index($leagueid, $seasonid)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://fulltime.thefa.com/api/sitecore/LeagueDetails/LeagueDivisionsFacet",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "leagueid=$leagueid&seasonid=$seasonid&filterSelection=&filtertype=primary&offset=0&recordperpage=500",
]);
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
$leagues = $json['response'];
DB::table('fa_leagues')
->where('league_id', $leagueid)
->update(['season_id_2020' => $seasonid]);
return view(
'frontend.football_leagues',
[
'league' => $leagues,
'seasonid' => $seasonid,
'leagueid' => $leagueid,
]
);
}
Exporting the fixtures
My initial exposure to packages and leveraging Composer for development came through the process of exporting fixtures. In the provided code snippet, I'm incorporating the widely-used Debugbar for Laravel. This choice was made to analyze the duration of waiting for a response from Full-Time compared to the application's data processing.
Within this project, another package I implemented is Laravel Excel. It was utilized in tandem with Livewire to facilitate seamless downloads on the page. Notably, during this period, Livewire was at version 1.0, making it a relatively new and intriguing experience to explore reactivity through PHP.
public function index($divisionid, $leagueid, $seasonid)
{
Debugbar::startMeasure('API', 'Time for Full-Time data Response');
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://fulltime.thefa.com/api/sitecore/DivisionDetails/DivisionFixturesFacet",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "divisionid=$divisionid&leagueid=$leagueid&TeamID=null&Days=all&seasonId=$seasonid&offSet=0&recordPerPage=500",
]);
$response = curl_exec($curl);
curl_close($curl);
Debugbar::stopMeasure('API');
$json = json_decode($response, true);
/**
* Get fulltime results to ensure we capture all fixtures
*/
$resultsCurl = curl_init();
curl_setopt_array($resultsCurl, [
CURLOPT_URL => "https://fulltime.thefa.com/api/sitecore/DivisionDetails/DivisionResultFacet",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "divisionid=$divisionid&leagueid=$leagueid&TeamID=null&Days=all&seasonId=$seasonid&offSet=0&recordPerPage=999",
]);
$resultsResponse = curl_exec($resultsCurl);
curl_close($resultsCurl);
$resultsJson = json_decode($resultsResponse, true);
return view(
'frontend.football_fixture',
[
'teams' => $json['LeagueFixture'],
'league' => $json,
'division_id' => $divisionid,
'season_id' => $seasonid,
'results' => $resultsJson['LeagueResults'],
]
);
}
public function export()
{
return Excel::download(new FixturesExport, 'fixtures.csv');
}