Curl cookies expires vs n/a

Asked

Viewed 229 times

5

The doubt is as follows, in the network browser console says cookies do not expire, see the image below:

inserir a descrição da imagem aqui

But when picking up (making a request of these) these cookies with curl expire:

ct0=e3197b1390ba24c4ae827fc6740344fa; Expires=Mon, 23 Oct 2017 14:21:33 UTC; Path=/; Domain=.twitter.com; Secure

Today the 14:21:33 they no longer work. How am I picking up these cookies?

Thus:

$obj = new stdClass;
$obj->cookies = '';
$obj->location = '';

$request = curl_init();
curl_setopt_array($request, [
        CURLOPT_URL                         => 'https://twitter.com/',
        CURLOPT_CUSTOMREQUEST       => 'GET',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_SSL_VERIFYHOST  => false,
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_HEADER                  => true,
        CURLOPT_COOKIEJAR               => getcwd() . '/cookies/' . $username . '.txt',
        CURLOPT_HEADERFUNCTION  => function($curl, $header) use (&$obj) {
            if (stripos($header, 'Set-Cookie:') === 0) {
                if (preg_match('/^Set-Cookie: \s*([^=]*)=([^;]*)/mi', $header, $matches)) {
                    $obj->cookies .= $matches[1] . '=' . $matches[2] . '; ';
                    $obj->{$matches[1]} = $matches[2];
                }
            }
            var_dump($header);
            return strlen($header);
        }
    ]
);
$response = curl_exec($request);

Whereas:

if (stripos($header, 'Set-Cookie:') === 0) {
    if (preg_match('/^Set-Cookie: \s*([^=]*)=([^;]*)/mi', $header, $matches)) {
        $obj->cookies .= $matches[1] . '=' . $matches[2] . '; ';
        $obj->{$matches[1]} = $matches[2];
    }
}

Already plan the work so they do not expire correct?

EDITED

In the documentation I translated and says the following:

Curl has a built-in cookie analysis engine that comes into use if you want to reconnect to a server and use cookies which were stored on a previous connection (or manually manually to trick the server into believing that you had a previous connection ). To use cookies previously stored, you executes Curl as:

Curl --cookie stored_cookies_in_file http://www.example.com

And here’s what I want:

if you want to reconnect to a server and use cookies that were stored in a previous connection

Ta how I do it in PHP ?

  • 1

    "Curl has a cookie analysis engine, "only the CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE, CURLOPT_COOKIELIST and the CURLOPT_COOKIESESSION use the cookie manager of Curl, the function above does not use, this has been said several times including. The code mentioned above completely ignores the expiration time, it only gets the name and value. Even, part of the questions may be answered here.

  • @Inkeliz, that’s correct, but what I don’t understand is why Instagram cookies I can use for several months, and Twitter can’t use after hours.

  • 1

    The equivalent ct0 of Instagram is the csrftoken, he doesn’t care about the value, as long as the csrftoken is the same header value x-csrftoken, if both are 1 will be valid, for example. Twitter may have another mechanism, updating the value periodically, including associated with session.

  • Yes @Inkeliz, I’ve monitored twitter there, expires in a few hours, while instagram expires in 2018

1 answer

2


Documentation on the PHP website about the CURLOPT_COOKIEFILE:

The name of the file containing the cookie data. The cookie file can be in Netscape format, or just Plain HTTP-style headers dumped into a file. If the name is an Empty string, no cookies are Loaded, but cookie Handling is still enabled.

And in that part:

If the name is an Empty string, no cookies are Loaded, but cookie Handling is still enabled.

Function in which the above citation is referenced:

bool curl_setopt ( resource $ch , int $option , mixed $value )

Then add CURLOPT_COOKIEFILE with the same address as CURLOPT_COOKIEJAR, so that cookies are loaded.

Clicking here for a response similar to yours. Clicking here for the documentation of curl options php

  • I did this, and stopped saving some cookies in the file .txt, then I withdrew, with the instagram So far they’ve all been working for two days, not twitter. But reading your answer I’m almost close, I’ll wait a little longer if I can solve based on your answer I’ll mark as solved ok? and pay the recomepnsa!

Browser other questions tagged

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