我想知道在另一个站点登录后是否可以自动登录moodle?我尝试使用 curl,但它似乎并没有真正起作用。
这是我的测试代码:
//set POST variables
$url = "http://moodlesite.com/login/index.php";
$fields = array(
'username' => 'username',
'password' => 'password'
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
我注意到在moodle登录到系统后,他们再次将其重定向到http://moodlesite.com/login/index.php?testsession=userid,在这种情况下他们需要cookies。