把curl回来的cookies处理成数组

WBOY
Release: 2016-06-13 13:00:34
Original
1233 people have browsed it

把curl返回的cookies处理成数组
目的只是将Curl返回的Header里面的Cookies转化成数组和获取Location

例子:
……
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=/
转成
array("B" => "1","C" => "5","R" => "5" )
并取出 Location 为 $l="http://example.com/";

谢谢了
------解决方案--------------------

$s = <<< TXT<br />
Cache-Control: private,no-cache="set-cookie"<br />
Expires: -1<br />
Pragma: no-cache<br />
Location: http://example.com/<br />
Set-Cookie: B=1; Path=/<br />
Set-Cookie: C=5; Path=/<br />
Set-Cookie: R=5; Path=/<br />
TXT;<br />
<br />
$res = array();<br />
foreach(preg_split("/[\r\n]+/", $s, -1, PREG_SPLIT_NO_EMPTY) as $row) {<br />
  switch($k = strtok($row, ':')) {<br />
    case 'Location':<br />
      $res[$k][] = trim(strtok(''));<br />
      break;<br />
    case 'Set-Cookie':<br />
      $res[$k][trim(strtok('='))] = trim(strtok(';'));<br />
      break;<br />
  }<br />
}<br />
print_r($res);
Copy after login
Array
(
    [Location] => Array
        (
            [0] => http://example.com/
        )

    [Set-Cookie] => Array
        (
            [B] => 1
            [C] => 5
            [R] => 5
        )

)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template