Jump to content

cURL login, go to other page and retrieve source

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
SimonBS

SimonBS

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I'm trying to make a PHP script which should login to a remote site the go to a subpage and get the source of this page.

The purpose is to retrive an image. This task shall be repeated for every student on my school (though I do believe it will only be necessary to login once, then go to the subpages).

My problem is, that I cannot get the script to login. I have not tried retrieving any images yet, since the first step must be to have the login process working.

I hope there's somebody out there who can help me login to this site and retrieve the source.

I'm trying to login to this form ('brugernavn' is username in Danish, 'adgangskode' is password):
https://www.lectio.d.../285/login.aspx

This is my code:
$curl_connection = curl_init('https://www.lectio.dk/lectio/285/login.aspx');	// establish connection


// cURL options

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);	// timeout 30 sec

curl_setopt($curl_connection, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');	// simulated browser

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);	// force cURL to not display the output but return as a string

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);	// does not trigger error when no SSL certificate

curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);	// follow directions from remote sites header

curl_setopt($curl_connection, CURLOPT_COOKIEJAR, 'cookie.txt');	// save cookie in cookie.txt


// data to submit to remote form

$post_data['m$Content$username2'] = '';	//username removed

$post_data['m$Content$password2'] = '';	// password removed


// format data to string (eg. key1=value1&key2=value2)

foreach ($post_data as $key => $value) {

	$post_items[] = $key . '=' . $value;

}

$post_string = implode ('&', $post_items);


// cURL options

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);	// post $post_string


print_r(curl_getinfo($curl_connection)); // print error check

echo '<hr />'.curl_errno($curl_connection).'-'.curl_error($curl_connection); // print error numbers


$result = curl_exec($curl_connection);	// execute

curl_close($curl_connection);	// close connection


// go to other page

$curl_connection = curl_init(); 

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl_connection, CURLOPT_COOKIEFILE, 'cookie.txt'); 

curl_setopt($curl_connection, CURLOPT_URL, 'https://www.lectio.dk/lectio/285/SkemaNy.aspx?type=elev&elevid=1628118635');


$result = curl_exec($curl_connection);	// execute

curl_close ($curl_connection);	// close connection

echo $result;	// print source

I have made a file called 'cookie.txt' in the same folder as the script is in and given it the permissions 777.

This is the output of the above code ('log ind' means 'log in' in Danish - therefore the script has obviously not logged in):
Eleven Simon Binderup Støvring, 3z - Skema - Lectio - Mariagerfjord Gymnasium

I hope somebody can help me :-)

Thanks in advance,
Simon B. Støvring

#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Im not too fluent with php and cURL, but I think your going to have to run multiple curl executions within the script. You can read up about it here.

Someone else should be able to post back something more helpful, thats just my thoughts.

Also, you should post up usernames and passwords like that. Not exactly 'safe'

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3
SimonBS

SimonBS

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you very much for your help. I have read the link earlier but I have not been able to figure out a solution.
About the usernanem and password, you made me nervous but I do no think I have posted any of them.

// data to submit to remoteform 

$post_data['m$Content$username2'] = ''; //username removed 

$post_data['m$Content$password2'] = ''; //*password removed 

But thank you :-)

Edited by SimonBS, 22 October 2010 - 03:46 AM.