Retrieving All Premier League Matches from 2018/2019
In this tutorial, we will show you how easy it is to attain all of the matches from any given season. In this example, we'll use the English Premier League stats and matches from 2018/2019, where Manchester City claimed their 2nd sucessive Premier League title.
1: Establishing Your Connection
First, check that your API key is active and working. Hitting this URL, whilst adding your API key, should return a response of true
. If you have not got your API key yet, please visit your account page.
Basic API Call
https://api.football-data-api.com/league-teams?key=YOUR_KEY
2: Finding Matches Using The League ID
View method for exploring all data-points and IDs via our documentation page. For this tutorial, we will be listing all of the matches from the latest EPL season in a project that you will be able to download at the end of this tutorial.
API Call for League Matches
https://api.football-data-api.com/league-matches?key=example&league_id=1625
In this case, the ID for the latest EPL season is 1625
and we can access the matches from this season by using the league-matches
API call. Your JSON response will contain 380 games, all packed with stats ready for saving or manipulating. Pagination is available by adding &page=PAGE_NUMBER
to the end of your API call.
Example Match Object
{
id:453873,
homeID:149,
awayID:108,
season:"2018/2019",
status:"complete",
roundID:3547,
game_week:1,
revised_game_week:-1,
homeGoals:[ ],
awayGoals:[ ],
homeGoalCount:2,
"..."
}
3: Working With The Data
Once you have the response back from the server, you can manipulate the date to fit your needs. In this example, we will be looping through the games and displaying the matches in a table. To view all of the data-points available in each match object that's returned, please view our documentation.
For this tutorial, we'll be working in PHP. JSON can be manipulated by most programming languages. For more on working with JSON, we recommend these articles:
4: Saving Data to Variable
We have created league-matches.php
and inside this file we are going to utilise the file_get_contents()
function. This PHP function is used to read a file and convert into a string. Once we have that string, we will need to use json_decode()
to convert our string into an array that we can utilise and manipulate.
league-matches.php
$result = file_get_contents("https://api.football-data-api.com/league-matches?key=YOUR_API_KEY&league_id=1625");
$result = json_decode($result, true);
$matches = $result['data'];
Using var_dump()
on your new $matches
variable will show you all of the matches for this league. However, that's no use to anyone. In the next steps, we will loop through and manipulate the data.
5: Looping Through Matches
We'll start by creating a simple foreach
loop for $matches
that displays both team names.
league-matches.php
foreach($matches as $match):
echo "{$match['home_name']} vs {$match['away_name']}";
endforeach;
HTML Output
Manchester United vs Leicester City
Newcastle United vs Tottenham Hotspur
AFC Bournemouth vs Cardiff City
...
6: Adding More Data-Points to Your HTML
Now we have a working list of matches, we're free to manipulate it as we see fit. Now, what's the point of showing results without the scores? Let's add to our existing code.
league-matches.php
foreach($matches as $match){
echo $match['home_name'];
echo $match['homeGoalCount'];
echo $match['awayGoalCount'];
echo $match['away_name'];
echo "<br>"
}
HTML Output
Manchester United 2-1 Leicester City
Newcastle United 1-2 Tottenham Hotspur
AFC Bournemouth 2-0 Cardiff City
...
7: Filtering Matches
Now, let's add a condition to our $matches
loop to show only games that contained 3 goals or more.
league-matches.php
if($match['totalGoalCount'] >= 3){
echo $match['home_name'];
echo $match['homeGoalCount'];
echo $match['awayGoalCount'];
echo $match['away_name'];
echo "<br>"
} else {
// Do Something Else
}
HTML Output
Manchester United 2-1 Leicester City
Newcastle United 1-2 Tottenham Hotspur
Huddersfield Town 0-3 Chelsea
...
Thanks for Reading
If you're planning on building an application with this data, why not have filters for $match['date_unix']
and allow the user to view games by the week, or month. The possibilities are endless. We hope you found this tutorial easy to understand and are now able to create your own applications using the FootyStats API. You can download the league-matches.php
file using the button in the sidebar to start testing on your computer.