snoopy.class and fileupload

snoopy.class and fileupload

by Kathrin Schwarz -
Number of replies: 6
Hi,
we try to automatize fileupload.
We use snoopy.class.php and curl.
We have no Problem to login in, but with the fileupload.
Here a very simple script...
Have someone an idea?
Thanks a lot
Kathrin

<?php
include "Snoopy.class.php";
$snoopy = new Snoopy;
//start a session
session_start();
//some variables
$user = "user1";
$pass = 'passwd';
$loginurl = "http://testlearn.uni-heidelberg.de/login/index.php";
$contenturl = "http://testlearn.uni-heidelberg.de/files/index.php?id=546";
//check if there is already a session, if not login and set a session
if (!$_SESSION['orion']) {
$submit_url = $loginurl;
$submit_vars["username"] = $user;
$submit_vars["password"] = $pass;
$submit_files["userfile"] = "C:\\xampp\htdocs\datei.html";
$submit_vars["action"] = 'upload';
$submit_vars["id"] = '546';
$submit_vars["wdir"] = '/';
$submit_url="http://testlearn.uni-heidelberg.de/files/index.php?choose=&id=546&wdir=%2F&action=save";
$snoopy->submit($submit_url,$submit_vars);
//logged in, now set session
$_SESSION['moodle'] = $snoopy->cookies["ASPSESSIONIDCSRBDRDT"];
$snoopy->submit($submit_url,$submit_vars, $submit_files);
if($snoopy->fetch($contenturl))
{
//do stuff here
$tmp=$snoopy->results;
echo $tmp;
}
else
echo "error fetching document: ".$snoopy->error."\n";
} else {
//so our session is already set, let's see if it's still valid
$snoopy->cookies["ASPSESSIONIDCSRBDRDT"] = $_SESSION['moodle'];

if($snoopy->fetch($contenturl))
{
$tmp=$snoopy->results;
//scrape the page to see if our session is still valid
//ok, our old session is no good, re-auth and set new session
if (preg_match("/\bAccount or User ID\b/", $tmp)) {
$submit_url = $loginurl;
$submit_vars["username"] = $user;
$submit_vars["password"] = $pass;
$snoopy->submit($submit_url,$submit_vars);
$_SESSION['moodle'] = $snoopy->cookies["ASPSESSIONIDCSRBDRDT"];
}
}}
Average of ratings: -
In reply to Kathrin Schwarz

Re: snoopy.class and fileupload

by Richard Enison -

KS,

I'm not familiar with Snoopy but I couldn't help noticing a couple of things about the script. I'm surprised you didn't get parse errors.

  1. I trust Snoopy.class.php is in your include_path.
  2. In the userfile definition line, you doubled the first \ but not the others. I believe they all need to be doubled (escaped).

RLE

In reply to Richard Enison

Re: snoopy.class and fileupload

by Kathrin Schwarz -
Thank you!
Yes and there is something wrong with the "temp" ....
Perhaps I have to write a new method for snoopy.class.php ....
Now I will try it with www::mechanize.
Thank you for your answer.
Kathrin
In reply to Richard Enison

Re: snoopy.class and fileupload

by Kathrin Schwarz -
We have it. It was not possible with php, but with java. It wasn't easy but I had some help.
Sincerely
Kathrin

In reply to Kathrin Schwarz

Re: snoopy.class and fileupload

by youri youri -
hi Kathrin Schwarz
thnx for your article
,but can you help me to develop a code to only log in and fetch data

thnx
In reply to youri youri

Re: snoopy.class and fileupload

by youri youri -
thank you for replying my message

I tried to develop a code to log in our forum and fetch data from this topic

<?php
include "snoopy.class.php";
$snoopy = new Snoopy;
//start a session
session_start();
//some variables
$user = "here the username";
$pass = 'here the password';
$loginurl = "http://moodle.org/login/index.php";
$contenturl = "http://moodle.org/mod/forum/discuss.php?d=85001";
//check if there is already a session, if not login and set a session
if (!$_SESSION['orion']) {
$submit_url = $loginurl;
$submit_vars["username"] = $user;
$submit_vars["password"] = $pass;
$snoopy->submit($submit_url,$submit_vars);
//logged in, now set session
$_SESSION['orion'] = $snoopy->cookies["ASPSESSIONIDCSRBDRDT"];
}

if($snoopy->fetch($contenturl))
{
$tmp=$snoopy->results;
echo $tmp;
}
?>

and i've typed the username and the passworf in the code
but the result was the page of logging in
not the page of this topic

this means the code couldn't login or make cookie

plz help me to fix the code ..

thnx
In reply to Kathrin Schwarz

Re: snoopy.class and fileupload

by Diva Diva -

1.

$snoopy->set_submit_multipart();

$submit_files["userfile"] = "C:\\xampp\htdocs\datei.html";

...

...

$snoopy->submit($submit_url,$submit_vars, $submit_files);

2.

Incoming server:

$file_dir='./uploaded_files';

$success_upload = move_uploaded_file($_FILES['userfile']['tmp_name'],$file_dir.'/'.$_FILES['userfile']['name']);