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

curl-and-php

Re: Retrieving cookies

From: Daniel Stenberg <daniel-curl_at_haxx.se>
Date: Thu, 22 Apr 2004 08:14:38 +0200 (CEST)

On Wed, 21 Apr 2004, Adam Bodnar wrote:

> Now I've read thru all the documentation and all I found was mention of
> CURLOPT_COOKIEFILE and then some examples with CURLOPT_COOKIEJAR. Now the
> former seems to be entirely related to sending a file with the cookie
> information you want to pass to a Curl session and the latter seems to be
> something where you can store the cookies

Simply put:

        CURLOPT_COOKIEFILE specifies a file to read cookies from

        CURLOPT_COOKIEJAR specifies a file to write cookies to

> (despite the lack of documentation on this on the PHP site).

I would say that the php/curl module is very poorly documented. People often
go for the docs on curl.haxx.se/libcurl/c/ instead since the differences
between the pure C interface and the PHP binding's API are small enough to
often not get noticed.

I personally put a great deal of effort on the libcurl docs to keep the
accurate, up-to-date and explanatory. I am not involved in the PHP project.

> I would prefer that I could set a variable to the cookie(s), but from a bug
> I read on the PHP site, it seems this is not possible yet.

It is not possible yet, and there is no time frame set for adding this ability
either. Given the lack of interest from people to help me implement it, I
wouldn't hold my breath for it happening at all. At least not this year.

For the curious-minded, here's the latest edit of the suggested API for cookie
stuff in libcurl: http://curl.haxx.se/dev/COOKIES

> So I messed around with this a bit and with the intention that any cookie
> information would be written to the filename(/tmp/my_cookies.txt) I sent
> CURLOPT_COOKIEJAR to so I then could read the information out of the file.
> However, the file was never written.

The CURLOPT_COOKIEJAR file is written when the curl handle is closed. If
libcurl has no cookies at that time, it won't create any file. You should also
make sure you have the correct permission on the directory etc.

> 1) Is there no way to get the cookie information that is set by a curl
> session into a variable?

Not without going through the hassle of storing then in a temp file.

> 2) If 1 is not possible, can you write it to a file? If so, then what
> am I doing wrong.

Yes you can use CURLOPT_COOKIEJAR as mentioned above.

> 3) If 1 and 2 aren't possible or it makes sense, should I just parse the
> header information?

You can parse the headers, sure.

-- 
     Daniel Stenberg -- http://curl.haxx.se -- http://daniel.haxx.se
      Dedicated custom curl help for hire: http://haxx.se/curl.html
Received on 2004-04-22