0

I want to capture cookies in php as array. I have tried everything but not getting the desired response. This is the cookie I want to capture.

[16] => set-cookie: GPS=1; Domain=.youtube.com; Expires=Sun, 15-Aug-2021 11:55:26 GMT; Path=/; Secure; HttpOnly
[17] => set-cookie: YSC=W73VfvUzd-g; Domain=.youtube.com; Path=/; Secure; HttpOnly; SameSite=none
[18] => set-cookie: VISITOR_INFO1_LIVE=b3-V28YjWWw; Domain=.youtube.com; Expires=Fri, 11-Feb-2022 11:25:26 GMT; Path=/; Secure; HttpOnly; SameSite=none

Note: I'm only want to capture set-cookie values not the rest of response headers.

I'm trying to make it like this.

[YSC] => wy0qYwLnnRc
[Domain]= .youtube.com
[Path]= /
[Secure] => 
[HttpOnly] => 
[SameSite] => none

So far I've tried this but don't know how to extract keys and values properly. Any help would be greatful.

$url = "https://www.youtube.com/";
$curlObj = curl_init();
curl_setopt($curlObj,  CURLOPT_URL,  $url);
curl_setopt($curlObj,  CURLOPT_RETURNTRANSFER,  1);
curl_setopt($curlObj,  CURLOPT_HEADER,  1);
curl_setopt($curlObj, CURLOPT_NOBODY, 1);
$result = curl_exec($curlObj);
$h = array_filter(explode("\n", $result), 'trim');
if (is_array($h)) {
    foreach ($h as $l) {
        if ((strpos($, 'set-cookie') !== false)) {
            $cookies['Cookies'] = explode(": ", $);
        }
    }
}
Mujtaba
  • 260
  • 1
  • 16
  • Does this answer your question? [how to get the cookies from a php curl into a variable](https://stackoverflow.com/questions/895786/how-to-get-the-cookies-from-a-php-curl-into-a-variable) – Sherif Aug 16 '21 at 05:05
  • I have already searched these questions and none of them answeres my question. That's why I've posted a new question!! – Mujtaba Aug 16 '21 at 15:57

0 Answers0