我想知道您是否可以使用 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;
?>