Hi Guys!
Basically i would like u to tell me a way how to log in to drupal web and then do some stuff, i have googled a lot, saw a lot of info about this and nothing helped.
I am already trying to figure this stuff out for an entire week. What i want is to log in to drupal website with curl, then go onto next pages while logged in.
So what i did was: I looked at the login form, saw that there are 2 hidden inputs, filled my $postData with login, pass, form_id and form_build_id. Form_build_id is taken from the web each time with fopen(). And i do log in successfully, at least when i put into my curl querry:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
It shows that i am logged in.
I do use curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); at this point.
Then i close curl. Next i initiate it again with different url, use curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); and when i execute it tells me that i am not logged in. I totally cant figure out what is going on. I look at this cookie.txt file, there is session name and sessionid, when i look at the header of my page it has setcookie of some different session name and sessionid....

Comments

jaypan’s picture

Do you have control over the external site you are accessing?

Contact me to contract me for D7 -> D10/11 migrations.

dorkster’s picture

Yes i do...
I decided to use curl in order to make some things more automatic, like adding menus and pages etc.
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "$postData");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$store = curl_exec ($ch);
curl_close($ch);
that is the code for logging in and it works...
Next using javascript i make my browser to open another curl file....
$curl_connection = curl_init($destination);
curl_setopt ($curl_connection, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_HEADER, 1);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
$result = curl_exec($curl_connection);
print_r(curl_getinfo($curl_connection));
curl_close($curl_connection);
This script i use in order to acess admin page, and it says that i cant do that....

jaypan’s picture

If you have server access to the external site, then you are best off using the services module on that site to provide a REST api from which you can access data. The services module already comes with methods for authentication (logging in), which will save you re-inventing the wheel. You are going to have troubles submitting form data with curl, because forms require the unique form ID that is generated when Drupal generates the form, and since you aren't generating the form, that ID doesn't exist.

Contact me to contract me for D7 -> D10/11 migrations.

dorkster’s picture

Thank you for help...
Im going to learn more about this services thing and rest server ....