Hi
I need help writing a curl script that will log into Moodle and download the quiz results as a CSV file.
This is what I got so far:
<?php
// HTTP authentication to moodle server
$url = "http://10.200.0.215/login/index.php"
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, username:password");
<?>
What additional commands do I need to process the download URL for the quiz results.
Thanks again.
I would suggest you search the code for other places that CURL is used.
Ah! and when you do, you find the download_file_content function in lib/filelib.php, which will get the contents of a remote URL using CURL.
That looks like a good start. The only problem is that it does not do the login bit.
Hmm. I suppose there is another whole approach you could try. You could copy the code from mod/quiz/report.php that does
And trick that into generating the CVS you want. You may be able to use something like output buffering to capture the result.
Oh, no! the display method calls exit, so that won't work. I wonder if the refactoring that has gone on in Moodle 2.0 makes this idea any easier?
Ah! and when you do, you find the download_file_content function in lib/filelib.php, which will get the contents of a remote URL using CURL.
That looks like a good start. The only problem is that it does not do the login bit.
Hmm. I suppose there is another whole approach you could try. You could copy the code from mod/quiz/report.php that does
include("report/default.php"); // Parent class
include("report/overview/report.php");
$report = new quiz_report();
$report->display($quiz, $cm, $course)
And trick that into generating the CVS you want. You may be able to use something like output buffering to capture the result.
Oh, no! the display method calls exit, so that won't work. I wonder if the refactoring that has gone on in Moodle 2.0 makes this idea any easier?
You're really writing a PHP script, not a curl script!
Below is some code that should help. Moodle requires you to manage cookies and session keys, making it quite tricky, so I got someone smarter than me to figure it out. The steps are as follows:
1. Create temporary file to remember cookies between curl calls.
2. Go to Moodle login page to get initial cookie.
3. Log in using your credentials.
4. Extract the session key assigned from the response (you need this only if you are submitting a form).
5. Go to the page you want to view/download.
6. Save the page, or submit a form, and save the result.
<?php
// Define a temporary file for holding cookies
$cookiefile = tempnam($working_folder, "cookies");
// 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);
// login to Moodle
$postfields = array(
'username' => $username,
'password' => $password,
'testcookies' => '1'
);
$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_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];
// Etc. ...
Below is some code that should help. Moodle requires you to manage cookies and session keys, making it quite tricky, so I got someone smarter than me to figure it out. The steps are as follows:
1. Create temporary file to remember cookies between curl calls.
2. Go to Moodle login page to get initial cookie.
3. Log in using your credentials.
4. Extract the session key assigned from the response (you need this only if you are submitting a form).
5. Go to the page you want to view/download.
6. Save the page, or submit a form, and save the result.
<?php
// Define a temporary file for holding cookies
$cookiefile = tempnam($working_folder, "cookies");
// 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);
// login to Moodle
$postfields = array(
'username' => $username,
'password' => $password,
'testcookies' => '1'
);
$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_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];
// Etc. ...
Hi Eoin
Thank you very much.
This will put me on the right path.
Thanks again.
Thank you very much.
This will put me on the right path.
Thanks again.