2 Pluspunkte 0 Minuspunkte

Wie kann ich alle Cookies anzeigen die mir bei einem Curl Request gesendet wurden?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in the output
$response = curl_exec($ch);

curl_close($ch);

list($headers, $body) = explode("\r\n\r\n", $response, 2);

$headerLines = explode("\r\n", $headers);
$cookies = array();

foreach ($headerLines as $line) {
    if (strpos($line, 'set-ookie:') === 0) {
        $cookieLine = trim(substr($line, strlen('set-cookie:')));
        $cookieParts = explode(';', $cookieLine);
        $cookie = array_shift($cookieParts); // Extract the cookie name and value
        list($cookieName, $cookieValue) = explode('=', $cookie, 2);
        $cookies[$cookieName] = $cookieValue;
    }
}

foreach ($cookies as $name => $value) {
    echo "Cookie: $name=$value\n";
}
von  

1 Antwort

1 Pluspunkt 0 Minuspunkte

Hier ist ein Beispiel

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in the output
$response = curl_exec($ch);

curl_close($ch);

preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);

$cookies = array();

foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}

var_dump($cookies);

von (532 Punkte)