使用 cURL 下载 Moodle 主页
您好,我在登录和下载网页时遇到一些问题。 我想做的是登录到我学校的 Moodle 网站并将主页保存在字符串中,这样我就可以使用解析器来挑选某些信息并构建一个 gwt 应用程序。
这是我到目前为止所拥有的: 在moodleLogin.html
<html>
<body>
<form name="lms" action="moodlephp.php" method="post">
<input type="text" name="username" id="username" /><br/>
<input type="password" name="password" id="password" />
<input type="hidden" name="domain" id="domain" value="students.ltu.edu.au" />
<input type="submit" name="Login" id="Login" />
</form>
<div id="test"></div>
</body>
</html>
在moodlephp.php
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$domain = $_POST["domain"];
$postfields = array( "username" => $username, "password" => $password, "domain" => $domain );
$working_folder = "\cookies";
$cookiefile = tempnam($working_folder, "cookies");
$urlbase = "https://www.latrobe.edu.au/lms";
$agent = $_SERVER['HTTP_USER_AGENT'];
$debug = "";
$debughandle = "";
// get session cookies to set up the Moodle interaction
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . "/login/index.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
if(!$cookiefile) echo('empty');
else echo($cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . '/login/index.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec ($ch);
curl_close ($ch);
if (!$result || !preg_match("/HTTP\/1.1 303 See Other/", $result))
{
unlink($cookiefile);
header("HTTP/1.0 403 Forbidden");
die("Username/password incorrect.\n");
}
// get session key
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . "/question/import.php?courseid=" . $courseid);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);
curl_close ($ch);
if (!preg_match("/sesskey=(\w*)/", $result, $matches))
{
unlink($cookiefile);
header("HTTP/1.0 500 Internal Server Error");
die("Could not determine sesskey.\n");
}
$sesskey = $matches[1];
echo $result;
?>
任何帮助将不胜感激。我目前收到的只是“用户名/密码不正确”。
我一直在尝试遵循此处发布的代码 -> http://moodle.org/mod/forum/discuss.php?d=153580 提前致谢。
Hi I'm having some trouble with logging in and downloading a web page.
What i'm trying to do is log on to my school's moodle site and save the main page in a string so i can use a parser to pick out certain information and build a gwt app.
this is what I have so far:
in moodleLogin.html
<html>
<body>
<form name="lms" action="moodlephp.php" method="post">
<input type="text" name="username" id="username" /><br/>
<input type="password" name="password" id="password" />
<input type="hidden" name="domain" id="domain" value="students.ltu.edu.au" />
<input type="submit" name="Login" id="Login" />
</form>
<div id="test"></div>
</body>
</html>
in moodlephp.php
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$domain = $_POST["domain"];
$postfields = array( "username" => $username, "password" => $password, "domain" => $domain );
$working_folder = "\cookies";
$cookiefile = tempnam($working_folder, "cookies");
$urlbase = "https://www.latrobe.edu.au/lms";
$agent = $_SERVER['HTTP_USER_AGENT'];
$debug = "";
$debughandle = "";
// get session cookies to set up the Moodle interaction
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . "/login/index.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
if(!$cookiefile) echo('empty');
else echo($cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . '/login/index.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec ($ch);
curl_close ($ch);
if (!$result || !preg_match("/HTTP\/1.1 303 See Other/", $result))
{
unlink($cookiefile);
header("HTTP/1.0 403 Forbidden");
die("Username/password incorrect.\n");
}
// get session key
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_VERBOSE, $debug);
if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
curl_setopt($ch, CURLOPT_URL, $urlbase . "/question/import.php?courseid=" . $courseid);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);
curl_close ($ch);
if (!preg_match("/sesskey=(\w*)/", $result, $matches))
{
unlink($cookiefile);
header("HTTP/1.0 500 Internal Server Error");
die("Could not determine sesskey.\n");
}
$sesskey = $matches[1];
echo $result;
?>
Any help would be greatly appreciated. All I'm receiving at the moment is "Username/password incorrect."
I've been trying to follow the code posted here -> http://moodle.org/mod/forum/discuss.php?d=153580
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白你想要做什么,但这是一种非常低效的方法。更好的方法是使用 Web 服务通过 XMLRPC 或 SOAP 进行连接(使用 PHP 的内置库非常容易)并以这种方式请求您需要的信息。 Moodle 2.0 内置了一个 Web 服务堆栈 - 只需按照此处的说明启用它即可。 Moodle 1.9将需要你添加一个插件,其中有几个位于modules和plugins目录中。 OKTech Webservices 也许是最好的。
您实际上想从首页获得什么信息?
I think I see what you are trying to do, but this is a very inefficient way of doing it. A far better approach would be to use webservices to connect using XMLRPC or SOAP (very easy using PHP's built in libraries) and request the information you need that way. Moodle 2.0 has a webservice stack built in - just enable it as per instructions here. Moodle 1.9 will need you to add a plugin, of which there are several on the modules and plugins directory. OKTech Webservices is perhaps the best.
What information do you actually want from the front page?