Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL seems to be ignoring cookie jar/file

Tags:

php

curl

cookies

I'm using this....

    function cURL($url, $header=NULL, $p=NULL)
    {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/TestCookies");


        if ($p) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
        }
        $result = curl_exec($ch);

        if ($result) {
            return $result;
        } else {
            return curl_error($ch);
        }
        curl_close($ch);
    }

And I'm making a call like this...

        $Headers = array(
                "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*\/*;q=0.5",
                "Accept-Language: en-gb,en;q=0.5",
                "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7");


        $a = $this->cURL("https://www.mysite.com/",$Headers, null);

I'd expect this to receive a cookie and write it to the file /tmp/TestCookies.

The site definitely returns Set-Cookie headers - I can see them if I dump $a, however, the file in question is never created.

In case it's a permissions issue, I created it with touch and chmod 777'd it - The file now exists but it's empty.

What am I doing wrong?

like image 669
Basic Avatar asked Jan 15 '12 18:01

Basic


People also ask

How do you get the curl response to cookies?

By default, curl doesn't send any cookies but you can add your own cookies via the -b 'name=value' command line argument. To save cookies from the response to a file, use the -c file option. To load cookies from a file, use the -b file option.

Does curl have cookies?

curl has a full cookie "engine" built in. If you just activate it, you can have curl receive and send cookies exactly as mandated in the specs. tell curl a file to read cookies from and start the cookie engine, or if it is not a file it will pass on the given string.

How let curl use same cookie as browser in PHP?

The only way this would work is if you use persistent cookies in your curl request. CURL can keep cookies itself. Assign a session ID to the cookie file (in curl) so subsequent requests get the same cookies. When a user clicks a link, you will need to curl the request again.

What is Curl_ setopt?

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option.


1 Answers

The curl constants are split into

  • CURL_COOKIEFILE as the cookies.txt source which is read from
  • and CURL_COOKIEJAR as the datastore that is written back to

You do have to provide _COOKIEFILE with an empty string to enable it IIRC, but _COOKIEJAR if you want them stored. Normally set both options to the same name.

like image 191
mario Avatar answered Nov 12 '22 00:11

mario