无法从我的学校站点获取我的日程表数据。使用cURL登录将不起作用 [英] Unable to fetch my schedule data from my schools site. Login with cURL won't work

查看:55
本文介绍了无法从我的学校站点获取我的日程表数据。使用cURL登录将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:为什么要减去负号?

我要执行的操作如下:


  • 我正在尝试使用cURL登录到我的学校站点,并获取用于AI的时间表。

因此,我需要使用通行证和密码登录,但是学校网站上的表单也需要一个隐藏的令牌。

So I need to login using my pass and number, but the form on the school site also needs a hidden 'token'.

<form action="index.php" method="post">
    <input type="hidden" name="token" value="becb14a25acf2a0e697b50eae3f0f205" />
    <input type="text" name="user" />
    <input type="password" name="password" />
    <input type="submit" value="submit">
</form>

我能够成功检索令牌。然后我尝试登录,但失败。

I'm able to successfully retrieve the token. Then I try to login, but it fails.

// Getting the whole website
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.school.com');
$data = curl_exec($ch);

// Retrieving the token and putting it in a POST
$regex = '/<regexThatWorks>/';
preg_match($regex,$data,$match);
$postfields = "user=<number>&password=<secret>&token=$match[1]";

// Should I use a fresh cURL here?

// Setting the POST options, etc.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

// I won't use CURLOPT_RETURNTRANSFER yet, first I want to see results. 
$data = curl_exec($ch);

curl_close($ch); 

嗯...这不起作用...

Well... It doesn't work...


  • 令牌是否有可能在每个curl_exec中更改?因为该站点第二次无法识别脚本...

  • 我是否应该为第二部分创建新的cURL实例(?)?

  • 是否还有另一种方法可以在1个连接内获取令牌?

  • Cookies?

  • Is it possible the token changes every curl_exec? Because the site doesn't recognize the script the second time...
  • Should I create a new cURL instance(?) for the second part?
  • Is there another way to grab the token within 1 connection?
  • Cookies?

推荐答案

这就是我解决的方法。问题可能出在不使用Cookie部分。
这仍然可能是丑陋的代码,因此欢迎进行任何改进!

This is how I solved it. The problem was probably the 'not-using-cookies' part. Still this is probably 'ugly' code, so any improvements are welcome!

// This part is for retrieving the token from the hidden field.
// To be honest, I have no idea what the cookie lines actually do, but it works.
$getToken= curl_init();
curl_setopt($getToken, CURLOPT_URL, '<schoolsite>');       // Set the link
curl_setopt($getToken, CURLOPT_COOKIEJAR, 'cookies.txt');  // Magic
curl_setopt($getToken, CURLOPT_COOKIEFILE, 'cookies.txt'); // Magic
curl_setopt($getToken, CURLOPT_RETURNTRANSFER, 1);         // Return only as a string
$data = curl_exec($token);                                 // Perform action

// Close the connection if there are no errors
if(curl_errno($token)){print curl_error($token);}
else{curl_close($token);} 

// Use a regular expression to fetch the token
$regex = '/name="token" value="(.*?)"/';
preg_match($regex,$data,$match);

// Put the login info and the token in a post header string
$postfield = "token=$match[1]&user=<number>&paswoord=<mine>";
echo($postfields);

// This part is for logging in and getting the data.
$site = curl_init();
curl_setopt($site, CURLOPT_URL, '<school site');
curl_setopt($site, CURLOPT_COOKIEJAR, 'cookies.txt');    // Magic
curl_setopt($site, CURLOPT_COOKIEFILE, 'cookies.txt');   // Magic
curl_setopt($site, CURLOPT_POST, 1);                     // Use POST (not GET)
curl_setopt($site, CURLOPT_POSTFIELDS, $postfield);      // Insert headers
$forevil_uuh_no_GOOD_purposes = curl_exec($site);        // Output the results

// Close connection if no errors           
if(curl_errno($site)){print curl_error($site);}
else{curl_close($site);} 

这篇关于无法从我的学校站点获取我的日程表数据。使用cURL登录将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆