cURL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

cURL

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Hey, it works now ;-)

Thanks Wierdan,

-Nay
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.....
Last edited by Weirdan on Thu Jan 08, 2004 1:11 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 ;)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Ah, missed that part. Off to POP3 now! *goes*

-Nay
Post Reply