我尝试了这么多不同的事情,但没有找到任何解决方案,以便使用cUrl登录到http://www.espn.com/login/,登录后,我想检索一个受保护的密码区http://games.espn.com/ffl/clubhouse?leagueId=93772&teamId=1&seasonId=2018
我尝试使用的是:
$username = 'email';
$password = 'password!';
$loginUrl = 'http://www.espn.com/login/';
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
sleep(3);
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'http://games.espn.com/ffl/clubhouse?leagueId=93772&teamId=1&seasonId=2018');
//execute the request
$content = curl_exec($ch);
curl_close($ch);
//save the data to disk
file_put_contents('download.txt', $content);
但是脚本登录后再也没有检索到html!
发布于 2018-07-06 07:14:39
这个页面的登录过程远比你的代码所描述的要复杂得多,而且,这是非常不寻常的,这是我见过的第一个不使用cookies的网站登录系统(这是几乎通用的方法)--这是如何登录的:。
1:向https://registerdisney.go.com/jgc/v6/client/ESPN-ONESITE.WEB-PROD/api-key?langPref=en-US
发送带有Access-Control-Request-Method: POST
、Access-Control-Request-Headers: cache-control,content-type,conversation-id,correlation-id,expires,pragma
和Origin: https://cdn.registerdisney.go.com
头的OPTIONS
请求,它将以类似于correlation-id: 13fb884f-d873-4344-9f32-fdeaa64cec7c
的头进行响应,您必须解析并保存此correlation-id头。
2:这有点棘手,你需要生成一个conversation-id
,我为你提取了生成算法,它隐藏在精简的js中,它是由uuid函数生成的,如下所示:
function f() { return g() + g() + "-" + g() + "-" + g("4") + "-" + g((Math.floor(10 * Math.random()) % 4 + 8).toString(16)) + "-" + g() + g() + g() }
function g(e) { for (var t = Math.floor(65535 * Math.random()).toString(16), n = 4 - t.length; n > 0; n--) t = "0" + t; return e = ("" + e).substring(0, 4), !isNaN(parseInt(e, 16)) && e.length ? e + t.substr(e.length) : t }
function uuid(){return f();}
(因为您运行的是PHP,而不是javascript,所以必须将此代码转换为PHP。这是相当琐碎的,如果你知道javascript和PHP,我cba atm),输出看起来像conversation-id: e2e3b494-2b67-4fe1-b0a2-21e0f7fa84ff
,当它生成时,保存它。现在向https://registerdisney.go.com/jgc/v6/client/ESPN-ONESITE.WEB-PROD/api-key?langPref=en-US
发送一个POST请求,请求正文包含字符串null
,头文件为Referer: https://cdn.registerdisney.go.com/v2/ESPN-ONESITE.WEB-PROD/en-US?include=config,l10n,js,html&scheme=http&postMessageOrigin=http%3A%2F%2Fwww.espn.com%2Flogin%2F&cookieDomain=www.espn.com&config=PROD&logLevel=LOG&topHost=www.espn.com&cssOverride=https%3A%2F%2Fsecure.espncdn.com%2Fcombiner%2Fc%3Fcss%3Ddisneyid%2Fcore.css&responderPage=https%3A%2F%2Fwww.espn.com%2Flogin%2Fresponder%2F&buildId=16388ed5943
和Content-Type: application/json
,头文件为conversation-id: $conversation_id
和correlation-id: $correlation_id
(注意,referer-header是硬编码的,并且是假的,您不会被重定向到这个url,但是javascript代码被编写为伪造它,使其看起来像是重定向的)。奇怪的狗屎,对吧?)
响应将包含一个类似于api-key: MR6UpmRG0VAqAvKStLzof79sbb+1w/HHBextDPqktXf9eXoDfklhgRMFKBpOqt5j63AqoYFx0VHH7/eePYnuw9U7Rg1F
的API头,您必须解析并保存此api-key
密钥。
3:使用标头Access-Control-Request-Method: POST
、Access-Control-Request-Headers: authorization,cache-control,content-type,conversation-id,correlation-id,expires,pragma
和Origin: https://cdn.registerdisney.go.com
向https://ha.registerdisney.go.com/jgc/v6/client/ESPN-ONESITE.WEB-PROD/guest/login?langPref=en-US HTTP/1.1
发出OPTIONS请求
注意:它将在头文件中给你一个新的相关id标记,但是忽略它,它似乎不会在任何地方使用,旧的相关标记是你应该在下一个请求中继续使用的1。
4:向https://ha.registerdisney.go.com/jgc/v6/client/ESPN-ONESITE.WEB-PROD/guest/login?langPref=en-US
发出POST请求,正文必须用用户名和密码进行json编码,并且看起来像json_encode(array('loginValue'=>$username,'password'=>$password))
,头是Referer: https://cdn.registerdisney.go.com/v2/ESPN-ONESITE.WEB-PROD/en-US?include=config,l10n,js,html&scheme=http&postMessageOrigin=http%3A%2F%2Fwww.espn.com%2Flogin%2F&cookieDomain=www.espn.com&config=PROD&logLevel=LOG&topHost=www.espn.com&cssOverride=https%3A%2F%2Fsecure.espncdn.com%2Fcombiner%2Fc%3Fcss%3Ddisneyid%2Fcore.css&responderPage=https%3A%2F%2Fwww.espn.com%2Flogin%2Fresponder%2F&buildId=16388ed5943
、Content-Type: application/json
和Authorization: APIKEY $api_key
(注意,必须从大写字符串APIKEY开始,后面跟一个空格和您之前提取的实际API键)以及correlation-id: $correlation_id
、conversation-id: $conversation_id
和Origin: https://cdn.registerdisney.go.com
这里它使用一个json进行响应,假设如果登录成功,则该json的错误成员为空,但是例如,当用户名不存在时,error
对象将包含以下内容:Error: invalid_grant, Description: 'PROFILE.NOT_FOUND', Code: AUTHENTICATION_FAILED"}
(这意味着我使用的用户名foo
不存在。)
如果现在登录成功,我假设它会给您一个令牌或cookie,您可以将其提供给http://games.espn.com/ffl/clubhouse?leagueId=93772&teamId=1&seasonId=2018以检查页面是否已登录,但由于我没有有效的用户名/密码可供测试,因此我实际上无法确认这一点。
https://stackoverflow.com/questions/51105853
复制相似问题