***************************
$ch = curl_init();
$data = array('username' => 'harshal3', 'password' => 'harshal3', 'submit' => 'Login');
curl_setopt($ch, CURLOPT_URL, 'http://192.168.101.69/moodle199/login/index.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_exec($ch);
curl_close($ch);
*********************************
This is not working. Debugging revealed that it successfully redirects to http://192.168.101.69/moodle199 after authentication but on http://192.168.101.69/moodle199/index.php, the $USER object is getting lost and hence login fails at "require_login()".
Thoughts why the $USER object is getting lost??
Thanks..
I'm having almost the same problem... did you find some answer of WHY is this happening? T.T
Did you try giving it form headers? This is just a guess for you to test.
$httpheader=array('Content-type: multipart/form-data', ...);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
You can write a shell script or do the commands manually:
Login:
curl -b cookies.txt -c cookies.txt -d "username=user&password=pass" -i www.yourmoodletest.com/login.php
That would do a POST to www.yourmoodletest.com/login.php passing username=user&password=password as parameters. And cookies will be saved to cookies.txt
Then you can request a page over and over with the returned HTML:
curl -b cookies.txt -c cookies.txt -i www.yourmoodletest.com/index.php
Hope this help you.
Hi,
the same could apply to the original PHP code, adding the cookie handling as per your CLI example:
...
// -b/--cookie <name=data|file name>
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
// -c/--cookie-jar <file name>
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
...
Never tried but it should work: the temp cookie file, cookies.txt, should be removed before a new login will be required.
HTH,
Matteo
I tried the CookieJar option but it didn't work either.
I finally settled for a simple POST using simetric encription for both sides (and some other validations)
Hello,
Would you be so kind to share the solution?
Thanks
$error
I was getting same problem related to curl. I was trying to invoke REST APIs from one linux box to another through curl. But whenever i executed the curl command, I was getting redirected to the login url, instead of the API url. Following the steps mentioned by you solved this problem. Thanks
@Kamal, please post the solution, rather than just saying that it worked for you. Thank you.