0

First of all, I want to make clear that I've tried many different ways and read quite a lot of articles about this, but none worked completely.

I've got to the point, where I successfully get the cookies by cURL, but when I try to actually use them, it simply fails and says that I'm being redirected.

The point, where I want to get, is that I just want to log in, retrieve the data from my profile and then do stuff with it...

Here's where I am right now (it just saves the correct cookies, but doesn't return back MY post-login page):

$url="https://myschool.managebac.com/sessions/";

$postdata = "login=myname@myschool.com&password=password&commit=Sign-in&remember_me=1";

$cr = curl_init();
curl_setopt($cr, CURLOPT_URL, $url);
curl_setopt($cr, CURLOPT_POST, TRUE);
curl_setopt($cr, CURLOPT_HEADER, TRUE);
curl_setopt($cr, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cr, CURLOPT_POSTFIELDS, $postdata);  
curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie.txt');   
$header = curl_exec($cr); 
curl_close($cr); 

$cr2 = curl_init();
curl_setopt($cr2, CURLOPT_URL, 'https://myschool.managebac.com/');
curl_setopt($cr2, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($cr2, CURLOPT_HEADER, TRUE);
curl_setopt($cr2, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($cr2, CURLOPT_COOKIEFILE, 'cookie.txt'); 
$result = curl_exec($cr2);
curl_close($cr2);

I hope that there actually is a possible way, because Managebac is not that used application (mainly for IB students), but it would help me a great deal if I got this working.

I'm glad for every and each answer.

4

1 回答 1

0

好的,所以我设法做到了。

现在,它得到了想要的信息并且一切正常。

如果有人想要代码,这里是:

$url="https://myschool.managebac.com/sessions";
$postdata = "login=mylogin&password=mypassword&commit=Sign-in&remember_me=1";

$cr_curlopt = array(CURLOPT_URL => $url,
                    CURLOPT_POST => TRUE,
                    CURLOPT_HEADER => TRUE,
                    CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
                    CURLOPT_RETURNTRANSFER => TRUE,
                    CURLOPT_POSTFIELDS => $postdata,
                    CURLOPT_COOKIEJAR => 'cookie.txt');

$cr = curl_init();
curl_setopt_array($cr, $cr_curlopt);
$header = curl_exec($cr);

curl_close($cr); 

$cr2_curlopt = array(CURLOPT_URL => "http://myschool.managebac.com",
                    CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
                    CURLOPT_RETURNTRANSFER => TRUE,
                    CURLOPT_COOKIEFILE => 'cookie.txt');

$cr2 = curl_init();
curl_setopt_array($cr2, $cr2_curlopt);
$result = curl_exec($cr2);
curl_close($cr2);

print $result;

顺便说一句,我不知道为什么它实际上会有所帮助,但是在我将 curlopt 添加从单独的字符串更改为一个数组后它开始工作。(感谢 DanRedux)

于 2012-04-30T17:20:38.140 回答