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(": ", $);
}
}
}