The purpose of processing cookies returned by curl into an array is to convert Cookies in the Header returned by Curl into an array and obtain the Location. example :...... Cache-Control: & nbsp; private, no-cacheset-cookieExpires: & nbsp;-1 Pragma: & nbsp; no-cacheLocation: & nbsp; h processes cookies returned by curl into arrays
The purpose is to convert the Cookies in the Header returned by Curl into an array and obtain the Location
Example:
......
Cache-Control: private, no-cache = "set-cookie"
Expires:-1
Pragma: no-cache
Location: http://example.com/
Set-Cookie: B = 1; Path =/
Set-Cookie: C = 5; Path =/
Set-Cookie: R = 5; Path =/
Convert
Array ("B" => "1", "C" => "5", "R" => "5 ")
And retrieve the Location as $ l = "http://example.com /";
Thank you.
------ Solution --------------------
$s = <<< TXT
Cache-Control: private,no-cache="set-cookie"
Expires: -1
Pragma: no-cache
Location: http://example.com/
Set-Cookie: B=1; Path=/
Set-Cookie: C=5; Path=/
Set-Cookie: R=5; Path=/
TXT;
$res = array();
foreach(preg_split("/[\r\n]+/", $s, -1, PREG_SPLIT_NO_EMPTY) as $row) {
switch($k = strtok($row, ':')) {
case 'Location':
$res[$k][] = trim(strtok(''));
break;
case 'Set-Cookie':
$res[$k][trim(strtok('='))] = trim(strtok(';'));
break;
}
}
print_r($res);
Array
(
[Location] => Array
(
[0] => http://example.com/
)
[Set-Cookie] => Array
(
[B] => 1
[C] => 5
[R] => 5
)
)