cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

RE: [Cookie] Handle a servlet session.

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Fri, 18 Oct 2002 09:11:16 +0200 (MET DST)

On Thu, 17 Oct 2002, Fluteau Jerome ICM N PG U PLM A 2 wrote:

> FYI: I try to simulate the navigation within a site in order to post data
> on several forms.

Me done that hundreds of times.

> First problem: send the username and password to get the login page.

> => First of all, when I test CURLOPT_COOKIEFILE, nothing seems to be written
> in my cookie.txt.

CURLOPT_COOKIEFILE is for *reading* cookies, so I should hope the file isn't
written to because of this use.

> Is there something wrong in this code (my username and password are within
> $LOGINURL):
>
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_COOKIEFILE, 0);

Yes, passing file 0 looks very odd. I would say a file name needs to be a
string. Or is that a string in PHP?

> curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\tmp\cookie.txt");
> curl_setopt($ch, CURLOPT_URL,$LOGINURL);
> curl_exec ($ch);
> curl_close ($ch);
>
> => Concerning CURLOPT_FOLLOWLOCATION, I'm not sure to well understand how I
> could use it.

You tried reading the docs?

> Indeed I tried to make a simple request on another site (my mailbox) with
> my username and password and it worked well, I got the page after login, I
> didn't even need to use CURLOPT_FOLLOWLOCATION!

Well, that's good for you, but trying FOLLOWLOCATION on a site that doesn't
use Location: isn't very productive!

> The problem is that the servlet from my work site doesn't give directly the
> name of the login page as on my mailbox, there is previously several
> requests:

I don't think I follow you here.

> .../servlet/oapdev?page=oap_fset_after_login.jsp
> and then only:
> .../eng/oap_fset_after_login.jsp
> and then the 3 frames of the "after login page"...
>
> The frames management is not a problem, all I have to do is to put the
> right cookie in each request header. CURLOPT_COOKIEJAR should be
> appropriate for this kind of work, if I succeed in using it...;-)

Yes it is.

> => Another little question:

> Is it possible to make several request in only one curl_exec()?

No. The request is made by curl_exec(), not previously. The curl_setopt()
calls are just for setting options.

> I'm mean doing each request with the previous cookie, to simulate a
> navigation until the right page. The following code is surely wrong, but
> you get the idea...;-)
>
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL,"$LOGINURL");
> curl_setopt($ch, CURLOPT_COOKIEFILE, 0);
> curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
> curl_setopt($ch, CURLOPT_URL,".../page1.jsp");
> curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
> curl_setopt($ch, CURLOPT_URL,".../page2.jsp");
> curl_exec ($ch);
> curl_close ($ch);

I get your idea, and to repeat what I use to say here, with libcurl itself
you can do repeated requests using the same libcurl handle which makes things
like this easier without the need of temporary file storage. The PHP/CURL
module is however not written to allow that practise.

You need to improve the PHP/CURL code first, or stay with the more simple
approach of mulitple init/setopt/exec/close iterations.

> Besides is the code executed in the same order as the written code? In
> other words, is the curl_setopts order important?

setopt() sets options. The order is only important if two setopts set the
same option to different values.

-- 
 Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs.
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2002-10-18