php cURL set manual cookies php cURL set manual cookies curl curl

php cURL set manual cookies


You can use curl_setopt function with option CURLOPT_HTTPHEADER.

curl_setopt($ch, CURLOPT_HTTPHEADER, ["Cookie: mycookie=value;mycookie2=value2"]);

[Edit]

From this guide The Unofficial Cookie FAQ.

The layout of Netscape's cookies.txt file is such that each line contains one name-value pair. An example cookies.txt file may have an entry that looks like this:

.netscape.com TRUE / FALSE 946684799 NETSCAPE_ID 100103

Each line represents a single piece of stored information. A tab is inserted between each of the fields.

From left-to-right, here is what each field represents:

domain - The domain that created AND that can read the variable.

flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.

path - The path within the domain that the variable is valid for.

secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.

expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.

name - The name of the variable.

value - The value of the variable.


For answer your question

1) To modify a bit before sending do this:

$file = file('cookie.txt');$cookies = [];/* * From the guidelines that I linked and posted above each line must contain 7 "fields" separated by tab only (The text you posted in the question is not well formatted but with some tricks we can do this) */foreach ($file as $fileLine) {    $fileLine = preg_replace("/\s+/", "\t", trim($fileLine));    $fields = explode("\t", $fileLine);    if (count($fields) === 7) {        $cookies[] = [            'domain' => $fields[0],            'flag' => $fields[1],            'path' => $fields[2],            'secure' => $fields[3],            'expiration' => $fields[4],            'name' => $fields[5],            'value' => $fields[6],        ];    }}var_dump($cookies); // Here you have all lines from your cookie file and can modify the fields such name, value or something

2) For override the cookie.txt with your modify cookies array do this:

// Preserve Netscape format$cookies = implode(PHP_EOL, array_map(function($data){    return implode("\t", $data);}, $cookies));file_put_contents('cookie.txt', $cookies);

3) For send your new cookies from file do this (must be in Netscape format):

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