【问题标题】:Fantasy Premier League API PHP cURLFantasy Premier League API PHP cURL
【发布时间】:2019-07-04 12:53:54
【问题描述】:

我想知道您是否可以使用 PHP 为 cCURL 请求提供一些代码来帮助我,我正在尝试从 fpl api 中检索数据,以显示我的联赛排名。联赛积分榜 api 的 url 是 -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;

?>

【问题讨论】:

  • 如果您要使用 stackoverflow,您需要提供您的代码,我们或许可以修复它。但我们不会为您编写代码。
  • 很公平。我将包含我迄今为止编写的代码,但它不会返回任何内容。
  • 您的一般尝试看起来是正确的。您是否输出了 $response 以确保您没有被重定向并且令牌确实存在?
  • 我做了,它只是返回一个错误的布尔值:(
  • 你试过 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);以防万一发生重写。邮递员会检查它,但卷曲不会。

标签: php curl csrf-token


【解决方案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>';

?>

【讨论】:

  • 感谢您提出这个问题,@Dharman 我现在只会使用它进行测试,但是一旦我上线,我就会删除它,但它肯定是其他人,他们正在查看我的代码,应该知道,所以我将其纳入 cmets。我会看看你提供的链接。再次感谢:)
  • 自此发布以来,您是否进行了任何更改?因为我仔细尝试了这段代码,但我仍然得到“[详细] => 未提供身份验证凭据。”错误
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
相关资源
最近更新 更多