PHP code example of rastor / expected-goals-client

1. Go to this page and download the library: Download rastor/expected-goals-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rastor / expected-goals-client example snippets


use ExpectedGoalsClient\ExpectedGoalsClient;

$client = new ExpectedGoalsClient('Your API Key');

$countries = $client->getCountries(); // list of countries
$leagues = $client->getTournaments($countryId); // list of tournaments for specified country
$seasons = $client->getSeasons($leagueId); // list of seasons for specified tournament
$fixtures = $client->getFixtures($seasonId); // list of fixtures for specified season
$fixture = $client->getFixture($fixtureId); // get one fixture

foreach ($client->getCountries() as $country) {
    foreach ($client->getTournaments($country->id) as $league) {
        foreach ($client->getSeasons($league->id) as $season) {
            echo "{$country->name}. {$league->name} ({$season->name})\n";
            echo "=====\n";

            $seasonFixtures = $client->getFixtures($season->id);

            $expectedGoals = [];
            $minutes = [];
            $teamNames = [];
            foreach ($seasonFixtures as $fixture) {
                if (!isset($teamNames[$fixture->homeTeam->id])) {
                    $teamNames[$fixture->homeTeam->id] = $fixture->homeTeam->name;
                    $minutes[$fixture->homeTeam->id] = 0;
                }

                if (!isset($teamNames[$fixture->awayTeam->id])) {
                    $teamNames[$fixture->awayTeam->id] = $fixture->awayTeam->name;
                    $minutes[$fixture->awayTeam->id] = 0;
                }

                $minutes[$fixture->homeTeam->id] += $fixture->duration->firstHalf + $fixture->duration->secondHalf;
                $minutes[$fixture->awayTeam->id] += $fixture->duration->firstHalf + $fixture->duration->secondHalf;

                foreach ($fixture->events as $event) {
                    if (!$event->xg) {
                        continue;
                    }

                    if (!isset($expectedGoals[$event->teamId])) {
                        $expectedGoals[$event->teamId] = 0;
                    }

                    $expectedGoals[$event->teamId] += $event->xg;
                }
            }

            $result = [];
            foreach ($expectedGoals as $teamId => $value) {
                $result[$teamId] = ($value / $minutes[$teamId]) * 90;
            }

            arsort($result);

            foreach ($result as $teamId => $value) {
                echo "$teamNames[$teamId]: {$value}\n";
            }

            echo PHP_EOL;
        }
    }
}