T
T
tincap2015-10-08 17:13:43
PHP
tincap, 2015-10-08 17:13:43

How to get data from cookie.txt?

We all know that in order to use cookies in Curl, you need to use the following parameters

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

Cookie example.txt
site.com      FALSE      /      FALSE      1475762564      csrftoken    efe1ce7900138db355aec682c44d21a5

How to easily and quickly parse cookie.txt so that the data is returned in an array or something else? Or do you have to do everything manually?
I had to do everything manually

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tincap, 2015-10-08
@tincap

public static function parsingCookieFile($cookieFile)
    {
        $handle = fopen($cookieFile, 'r');

        for ($i = 0; $i < 4; $i++) {
            if (!feof($handle)) {
                fgets($handle);
            }
        }

        $out = [];

        while (!feof($handle)) {
            $buffer = fgets($handle);

            preg_match('/\tFALSE\t\/\tFALSE\t[0-9]+\t([^\t]*)\t(.*)/', $buffer, $found);

            if (isset($found[1]) && isset($found[2])) {
                $out[$found[1]] = $found[2];
            }
        }

        fclose($handle);

        return $out;
    }

M
Maxim Timofeev, 2015-10-08
@webinar

Make cookie.php instead of cookie.txt, and in it

return [
ваш массив
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question