Page 1 of 1

cURL

Posted: Wed Jan 07, 2004 6:21 pm
by Nay
A few months back I brought up the topic of making a custom 'home page'. Complete with checking new messages on your mail server and new messages/posts on this lovely forums.

I got back into doing it. I just installed cURL on my localhost. I hope it went right anyhow. Here's what I got:

Code: Select all

<?php

$url = "http://forums.devnetwork.net/login.php";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=nay&password=2f915aec04d70cf4c46d32a85fc4b036");

$result = curl_exec($ch);
curl_close($ch);

print $result;

?>
I've tried with the password in it's original format AND md5-ed format as of here. It only gets me to the log in form. Any Idea?

-Nay

Posted: Wed Jan 07, 2004 7:48 pm
by Weirdan
passwords need to be sent in plaintext and you need to include `login` field into the postfields list:

Code: Select all

curl_setopt($ch, CURLOPT_POSTFIELDS, "username=nay&password=2f915aec04d70cf4c46d32a85fc4b036&login=Login");
Also perhaps you need to store a session id cookie you recieve for further access to the forum.

Posted: Wed Jan 07, 2004 8:26 pm
by Nay
Hey, it works now ;-)

Thanks Wierdan,

-Nay

Posted: Wed Jan 07, 2004 9:56 pm
by Nay

Code: Select all

<?php

$url = "http://forums.devnetwork.net/login.php";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=nay&password=123456&login=Login");

$result = curl_exec($ch);
curl_close($ch);

$result = explode("\n", $result);
$found = '';

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

$search = strstr($value, "View posts since last visit&nbsp;");

if(!empty($search)) {
   $found = $key;
} else {
   // do nothing
}

}

echo $result[$found];

?>
Now something tell's me that that's not so pratical do it that way =\. Any suggestions? I <insert-word-here> at regular expressions.

-Nay

Posted: Thu Jan 08, 2004 12:59 am
by Weirdan
I think your approach will be faster, but regexp is more readable in this case IMO:

Code: Select all

$url = "http://forums.devnetwork.net/login.php"; 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); // set url to post to 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($ch, CURLOPT_POST, 1); // set POST method 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=nay&password=123456&login=Login"); 

$result = curl_exec($ch); 
curl_close($ch); 
if(preg_match("|View posts since last visit&nbsp;\(([0-9]+)|",$result,$matches)){
  echo "Found ".$matches[1]." new posts";
}
if you need the new post count.....

Posted: Thu Jan 08, 2004 1:06 am
by Nay
Ah, thanks again Wierdan...

I was wondering how to actually 'get' the match with regular expressions. Anyhow, might I ask what exactly did the login=login do?

-Nay

Posted: Thu Jan 08, 2004 1:09 am
by Weirdan
In your form handling pages you probably used something like:

Code: Select all

if(isset($_POST['submit'])){
   //process form
 }else{
   //do nothing
 }
i.e. form processed only if submit button is pressed. `login` is that submit button ;)

Posted: Thu Jan 08, 2004 1:12 am
by Nay
Ah, missed that part. Off to POP3 now! *goes*

-Nay