curl-and-php
Re: cookiejar problem
Date: Mon, 11 Mar 2002 10:54:39 +0100 (MET)
On Mon, 11 Mar 2002 theexperts_at_allprodirect.com wrote:
> Can anyone explain why the cookies are not working for this script? I
> couldn't find much documentation on how to implement cookies in cURL.
Feel encouraged to help us doing this, as you seem to have experienced some
cookie stuff by now.
I have added details on cookie usage in the libcurl-the-guide document, with
an angle on how to use it with the C interface but it applies pretty cleanly
on the PHP point of view as well. I would of course appreciate if you read it
and added whatever you think should be added/corrected.
> Am I doing something wrong?
At a quick glance, it looks fine.
> function sendtohost($url,$data,$post) {
> $referer = $url;
> $useragent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)';
> $cookiefile = '/myfilepath/cookies.txt';
> $ch = curl_init();
> curl_setopt ($ch, CURLOPT_URL, $url);
> curl_setopt ($ch, CURLOPT_REFERER, $referer);
> curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
> curl_setopt ($ch, CURLOPT_POST, $post);
> curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
> curl_setopt ($ch, CURLOPT_HEADER, 1);
> curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
> curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
> curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
> curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
> curl_setopt ($ch, CURLOPT_VERBOSE, 1);
> $result = curl_exec ($ch);
> curl_close ($ch);
> return $result;
> }
>
> Background info: I need to access 4 sites from my server script. The scipt
> works fine for 2 of them. I figured out that the other 2 sites require
> cookie transactions. They will not work even from a browser without cookies
> enabled. With cookies disabled, Internet Explorer just follows the
> suggested link back to the home page.
>
> A Netscape cookie file with the required cookies was created by browsing to
> the sites in Netscape 6.2. This file was then uploaded. The path to the
> cookie file is correct since the script can read the file. Full file
> permissions have been tried. My script still gets a response like the
> following:
>
> HTTP/1.1 100 Continue Server: Microsoft-IIS/4.0 Date: Mon, 11 Mar 2002
> 09:54:14 GMT HTTP/1.1 302 Object Moved Location: /index.cfm Server:
> Microsoft-IIS/4.0 Content-Type: text/html Content-Length: 153 ...etc.
>
> Strangely, cURL is silent even with VERBOSE set. Also, there are no errors
> (curl_errno == 0; curl_error == ''). Maybe curl is having a problem writing
> to the file? But then wouldn't there be an error? My server is running:
> Apache/1.3.3 (Unix) PHP/4.1.2 libcurl 7.9.3 (OpenSSL 0.9.5)
I can't tell you exactly where the problem is here, but I can offer some
advice on how to get closer to where it is:
- Do the exact same operation with the command line tool. It often helps to
rule out problems with apache, the php-curl module and similar.
- The fact that VERBOSE is completely silent is very strange. It should show
the full HTTP requests and more.
- I'm afraid that no, the failing of writing the cookie jar will not cause an
error (I just checked the code). This should be corrected for future
versions.
- Check the HTTP headers you receive for incoming cookies, and make sure they
appear in the saved cookie jar. They should.
- Please upgrade to curl 7.9.5, we did yet a few more cookie parser fixes in
there and we did fix several SSL related problems that you may or may not
have been bitten by here.
-- Daniel Stenberg -- curl groks URLs -- http://curl.haxx.se/Received on 2002-03-11