cURL in Moodle

cURL in Moodle

by alojz domanicky -
Number of replies: 0

Hello,

I need to start quiz attempt using cURL, but I am stucked. This is my code : 

$ch = curl_init('http://mydomain.com/mod/quiz/startattempt.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1');
curl_setopt ($ch, CURLOPT_POSTFIELDS, "cmid={$moduleId}&sesskey=".sesskey());
curl_setopt($ch, CURLOPT_COOKIE, 'MoodleSession=b42b0483d8fe2059bc278d537f32edb5;');
curl_exec ($ch);
curl_close ($ch);

It sends all required POST data and my current session ID to that script (I checked it using $_REQUEST) and data are completely same as data that are normally sent using form, but it just throws this error "Timed out while waiting for session lock.
Wait for your current requests to finish and try again later.".

I tried to add some headers so it would look like request from real browser, but still got the error.

$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$ch = curl_init('http://mydomain.com/mod/quiz/startattempt.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "cmid={$moduleId}&sesskey=".sesskey());
curl_setopt($ch, CURLOPT_COOKIE, 'MoodleSession=b42b0483d8fe2059bc278d537f32edb5;');
curl_exec ($ch);
curl_close ($ch);

 Any ideas? Thanks

Average of ratings: -