if ($game === null) {
                    $game = Game::getGameByGameSecondTeamId($pdo, $team->getTeamId());
                }
                //get player statistic by game
                //response from api
                for ($week = 1; $week <= 21; $week++) {
                    $response = file_get_contents("https://api.fantasydata.net/nfl/v2/JSON/PlayerGameStatsByPlayerID/{$season}/{$week}/{$player->PlayerID}", false, $context);
                    $statisticData = json_decode($response);
                    //adds statistic to database
                    foreach ($stats as $statisticName) {
                        $statistic = Statistic::getStatisticByStatisticName($pdo, $statisticName);
                        if ($statistic === null || $statistic->getSize() <= 0) {
                            $statistic = new Statistic(null, $statisticName);
                            $statistic->insert($pdo);
                        } else {
                            $statistic = $statistic[0];
                        }
                        if (empty($statisticData->{$statisticName}) === false) {
                            $playerStatisticToInsert = new PlayerStatistic($game->getGameId(), $playerToInsert->getPlayerId(), $team->getTeamId(), $statistic->getStatisticId(), $statisticData->{$statisticName});
                            $playerStatisticToInsert->insert($pdo);
                        }
                    }
                }
            }
        }
    }
} catch (Exception $exception) {
    echo "Something went wrong: " . $exception->getMessage() . PHP_EOL;
} catch (TypeError $typeError) {
    echo "Something went wrong: " . $typeError->getMessage() . PHP_EOL;
}
Esempio n. 2
0
function getPlayers(string $league)
{
    try {
        // grab the db connection
        $pdo = connectToEncryptedMySQL("/etc/apache2/capstone-mysql/sprots.ini");
        $config = readConfig("/etc/apache2/capstone-mysql/sprots.ini");
        $apiKeys = json_decode($config["fantasyData"]);
        $opts = array('http' => array('method' => "GET", 'header' => "Content-Type: application/json\r\nOcp-Apim-Subscription-key: " . $apiKeys->{$league}, 'content' => "{body}"));
        $context = stream_context_create($opts);
        // response from api
        $response = file_get_contents("https://api.fantasydata.net/{$league}/v2/JSON/Players", false, $context);
        $data = json_decode($response);
        $sport = Sport::getSportBySportLeague($pdo, $league);
        /*todo
        
        		Get Teams
        		Get Game
        		Use the player's team key to get team IDs from your database
        		Use team ID to get games
        		Use game date time + player api ID to get game stats
        
        		*/
        // Adds players to database
        foreach ($data as $player) {
            $team = Team::getTeamByTeamApiId($pdo, $player->TeamID);
            if ($team !== null) {
                $playerToInsert = new Player(null, $player->PlayerID, $team->getTeamId(), $sport->getSportId(), $player->FirstName . " " . $player->LastName);
                $playerToInsert->insert($pdo);
                $game = Game::getGameByGameFirstTeamId($pdo, $team->getTeamId());
                if ($game === null) {
                    $game = Game::getGameByGameSecondTeamId($pdo, $team->getTeamId());
                }
                $gameDate = $game->getGameTime()->format("Y-m-d");
                // Get player statistics by game
                // response from api
                $response = file_get_contents("https://api.fantasydata.net/{$league}/v2/JSON/PlayerGameStatsByPlayer/{$gameDate}/{$player->PlayerID}", false, $context);
                $statisticData = json_decode($response);
                // Adds statistics to database
                foreach ($GLOBALS['stats'] as $statisticName) {
                    $statistic = Statistic::getStatisticByStatisticName($pdo, $statisticName);
                    if ($statistic === null || $statistic->getSize() <= 0) {
                        $statistic = new Statistic(null, $statisticName);
                        $statistic->insert($pdo);
                    } else {
                        $statistic = $statistic[0];
                    }
                    if (empty($statisticData->{$statisticName}) === false) {
                        $playerStatisticToInsert = new PlayerStatistic($game->getGameId(), $playerToInsert->getPlayerId(), $team->getTeamId(), $statistic->getStatisticId(), $statisticData->{$statisticName});
                        $playerStatisticToInsert->insert($pdo);
                    }
                    //					var_dump($statisticData);
                    //					var_dump($statisticName);
                    //					var_dump($statistic);
                    //					var_dump($statisticValue);
                }
            } else {
                echo "<p>* * * SIX OF THIRTEEN IS NOT A TEAM PLAYER * * *</p>" . PHP_EOL;
            }
        }
    } catch (Exception $exception) {
        echo "Something went wrong: " . $exception->getMessage() . PHP_EOL;
    } catch (TypeError $typeError) {
        echo "Something went wrong: " . $typeError->getMessage() . PHP_EOL;
    }
}