Cookies with Curl help

Asked

Viewed 920 times

1

I need to access a certain URL and load the file 'cookies.txt' the problem is the following I access the URL normally plus Cookies.txt does not load...

I think the problem is here in this part

$cookiepattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
preg_match_all($cookiepattern, $header_content, $matches); 
$cookiesOut = implode("; ", $matches['cookie']);

$Matches['cookie']); returns me an empty array...

file 'cookies.txt'

Netscape HTTP Cookie File

This file was generated by libcurl! Edit at your Own risk. .auto.com TRUE / FALSE 1452087781 ___suid 2ecfe4287cbeacd8399eaf98bec9ce0b.59089b9d033bc7c6dce8ea2fca139920

.auto.com TRUE / FALSE 1452865380 all7_user_region_confirmed 1 .auto.com TRUE / FALSE 1452865380 geo_location a%3A3%3A%7Bs%3A7%3A%22city_id%22%3ba%3A0%3A%7B%7Ds%3A9%3A%22region_id%22%3ba%3A1%3A%7Bi%3A0%3bi%3A89%3B%7Ds%3A10%3A%22country_id%22%3ba%3A0%3A%7B%7D .auto.com TRUE / FALSE 1423921380 autoru_sid ee094d60fa32eada_daf2da69dc79a59b7c8702a29554abbc .auth.auto.com TRUE / FALSE 1421329026 autoru_sid
.auth.auto.com TRUE / FALSE 1421329026 autoru_sid_key
.auto.com TRUE / FALSE 1421329026 cc6882cb6b6f0c912cf9589734fcc1e6
.auto.com TRUE / FALSE 1452865027 user_name Igor.savinkin5%40gmail.com .auto.com TRUE / FALSE 1452865027 username Igor.savinkin5%40gmail.com

until the moment I have the following,

iasdadsadsadsadafunction get_web_page( $url ){
    $options = array( 
    CURLOPT_RETURNTRANSFER => true, // to return web page
        CURLOPT_HEADER         => true, // to return headers in addition to content
        CURLOPT_FOLLOWLOCATION => true, // to follow redirects
        CURLOPT_ENCODING       => "",   // to handle all encodings
        CURLOPT_AUTOREFERER    => true, // to set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,  // set a timeout on connect
        CURLOPT_TIMEOUT        => 120,  // set a timeout on response
        CURLOPT_MAXREDIRS      => 10,   // to stop after 10 redirects
        CURLINFO_HEADER_OUT    => true, // no header out
        CURLOPT_SSL_VERIFYPEER => false,// to disable SSL Cert checks
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
    );

    $handle = curl_init( $url );
    curl_setopt_array( $handle, $options );

// additional for storing cookie 
    $tmpfname = dirname(__FILE__).'cookie.txt';
    curl_setopt($handle, CURLOPT_COOKIEJAR, $tmpfname);
    curl_setopt($handle, CURLOPT_COOKIEFILE, $tmpfname);

    $raw_content = curl_exec( $handle );
    $err = curl_errno( $handle );
    $errmsg = curl_error( $handle );
    $header = curl_getinfo( $handle ); 
    curl_close( $handle );

    $header_content = substr($raw_content, 0, $header['header_size']);
    $body_content = trim(str_replace($header_content, '', $raw_content));

// let's extract cookie from raw content for the viewing purpose         
    $cookiepattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
    preg_match_all($cookiepattern, $header_content, $matches); 
    $cookiesOut = implode("; ", $matches['cookie']);

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['headers']  = $header_content;
    $header['content'] = $body_content;
    $header['cookies'] = $cookiesOut;
return $header;}$header=get_web_page('http://www.minhaurl.com.br');print_r($header);

In short, I want to access a url and force the entry of cookies 'visited websites'

1 answer

0

All you need to do is use:

curl_setopt($handle, CURLOPT_COOKIEFILE, 'caminho/cookie.txt')

This will load the cookies that were previously saved, provided you specify the correct file. The CURLOPT_COOKIEJAR will save the cookies, the CURLOPT_COOKIEFILE will read saved cookies, as I have explained here.

If you save in this format then use this way, if you do not want to use Netscape format do not use CURLOPT_COOKIE_JAR. It makes no sense to use this if you want to extract the data otherwise or edit the data that has been saved.


You can also use the CURLINFO_COOKIELIST or the CURLOPT_HEADERFUNCTION to extract cookies. But, since you are already saving cookies using the COOKIEJAR it would be more appropriate to use the COOKIEFILE.

  • Hello good night, the cookies.txt file is in this format https://www.dropbox.com/s/zk5z0is7i3rcpdh/cookies.txt?dl=0 Please take a look... I changed how Voce said and it hasn’t worked yet

  • The curl_setopt must have come before the curl_exec. In addition, such file, which even contains several credentials, must have the cookies of the page that is connected, if there is no it will not use any cookie.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.