Fantasy Premier League API PHP cURL Fantasy Premier League API PHP cURL curl curl

Fantasy Premier League API PHP cURL


<?php// id of the league to show$league_id  = "your_league_id";// set the relative path to your txt file to store the csrf token$cookie_file = realpath('your_folder_dir_to_the_txt_file/cookie.txt');// login url$url = 'https://users.premierleague.com/accounts/login/';// make a get request to the official fantasy league login page first, before we log in, to grab the csrf token from the hidden input that has the name of csrfmiddlewaretoken$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);$response = curl_exec($ch);$dom = new DOMDocument;@$dom->loadHTML($response);// set the csrf here$tags = $dom->getElementsByTagName('input');for($i = 0; $i < $tags->length; $i++) {    $grab = $tags->item($i);    if($grab->getAttribute('name') === 'csrfmiddlewaretoken') {        $token = $grab->getAttribute('value');    }}// now that we have the token, use our login details to make a POST request to log in along with the essential data form header fieldsif(!empty($token)) {    $params = array(        "csrfmiddlewaretoken"   => $token,        "login"                 => "your_email_address",        "password"              => "your_password",        "app"                   => "plfpl-web",        "redirect_uri"          => "https://fantasy.premierleague.com/",    );    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_POST, true);    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);    /**     * using CURLOPT_SSL_VERIFYPEER below is only for testing on a local server, make sure to remove this before uploading to a live server as it can be a security risk.     * If you're having trouble with the code after removing this, look at the link that @Dharman provided in the comment section.     */    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    //***********************************************^    $response = curl_exec($ch);    // set the header field for the token for our final request    $headers = array(        'csrftoken ' . $token,    );}// finally, we now have everything we need to make the GET request to retrieve the league standings data. Enjoy :)$fplUrl = 'https://fantasy.premierleague.com/api/leagues-classic/' . $league_id . '/standings/';curl_setopt($ch, CURLOPT_URL, $fplUrl);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, false);if(!empty($token)) {    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}$response = curl_exec($ch);$league_data = json_decode($response, true);curl_close($ch);echo '<pre class="card">';    print_r($league_data);echo '</pre>';?>