2

我想知道您是否可以使用 PHP 为 cCURL 请求提供一些代码来帮助我,我正在尝试从 fpl api 中检索数据,以显示我的联赛排名。联赛积分榜 api 的网址是 - https://fantasy.premierleague.com/api/leagues-classic/my_league_id/standings/?page_new_entries=1&page_standings=1我可以通过浏览器查看数据,但是当我尝试检索它时对于 PHP 的 curl 请求,它会返回 403 错误,并显示消息“未提供身份验证凭据”。这意味着我需要登录凭据来检索它。

在使用开发工具和邮递员查看它之后,我现在知道我需要通过登录来获取一个 csrf 令牌,然后保存该令牌以在我请求联赛排名时使用。我不知道该怎么做,我有点做,但如果有人能为我试一试,我将不胜感激。

我需要做的是使用此表单数据向https://users.premierleague.com/accounts/login/发出 POST 请求 -

"login"         => "my_email",
"password"      => "my_password",
"app"           => "plfpl-web",
"redirect_uri"  => "https://fantasy.premierleague.com/",

发出请求后,我需要捕获 csrf 令牌 cookie,我相信它会在名为“csrfmiddlewaretoken”的隐藏输入中,并将其保存在变量中。

获得令牌并保存后,我将向https://fantasy.premierleague.com/api/leagues-classic/my_league_id/standings/发出 GET 请求,并将我保存的 csrf 令牌变量放入标题中,然后json 解码该数据,以便我能够回显联盟详细信息。

我很确定这是怎么做的,但是我在 PHP 方面不是那么好,并且想知道是否有任何可以帮助兄弟的品味。任何帮助将非常感激 :)

我从第一部分开始,提出了最初的发布请求,但在返回令牌方面没有运气。到目前为止,这是我的代码-

<?php

$cookie = "cookies.txt";
$url = 'https://users.premierleague.com/accounts/login/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

// var_dump($response);

$dom = new DOMDocument;
@$dom->loadHTML($response);

$tags = $dom->getElementsByTagName('input');
for($i = 0; $i < $tags->length; $i++) {
    $grab = $tags->item($i);
    if($grab->getAttribute('name') === 'csrfmiddlewaretoken') {
        $token = $grab->getAttribute('value');
    }
}

echo $token;

?>
4

1 回答 1

1
<?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 fields
if(!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>';

?>
于 2019-07-05T02:14:48.713 回答