我正在尝试使用curl登录moodle。任何人都可以告诉我代码中有什么问题。我已经制作了一个模块,其中我有一个提交按钮,这样当我按提交时它必须登录moodle。
function usmancurl_submit() {
$crl = curl_init();
$url = "http://localhost/moodle/login/index.php";
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($crl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_POST, 1);
$postdata = array(
"edit[name]" => "usman",
"edit[pass]" => "Usman5463",
"edit[form_id]" => "user_login",
"op" => "Log in"
);
// Tell curl we're going to send $postdata as the POST data
curl_setopt($crl, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($crl);
$headers = curl_getinfo($crl);
curl_close($crl);
if ($headers['url'] == $url) {
die("Cannot login.");
}
}