从头字符串中获取PHP Cookies

6

我有一个包含头部响应的字符串变量,就像这样

通过fsockopen()发送请求并获取头部,可以捕获此字符串。

$nn= "\r\n";
    do {
        $header .= fgets ( $fp, 16384 );
    } while ( strpos ( $header, $nn . $nn ) === false );

输出:

$header = "HTTP/1.0 200 OK Date: Tue, 13 Sep 2011 07:57:08 GMT Last-Modified: Tue, 13 Sep 2011 07:57:08 GMT 
Set-Cookie: EUID=face313a-dddd-11e0-a694-00000aab0f6c; 
expires=Mon, 08 Sep 2031 07:57:08 GMT; 
path=/; 
domain=.mysite.com; 
Set-Cookie: MIAMISESSION=face28e8-dddd-11e0-a694-00000aab0f6c:3493353428; 
path=/; 
domain=.mysite.com; 
Set-Cookie: USER_STATE_COOKIE=664d9ed4a110fa1deb0dc4cc2d7e76fa00a75a0b5c9b3fa8; 
path=/; 
domain=.mysite.com; 
Set-Cookie: MIAMIAUTH=f8d13d07cc4e8b0ef9904f5ce40367ce44c989661956ef1cd2804726b0dd5508db661a8f9582df19bc954c57d0f2c293760e4d5ddda88d556dbcfae8dd80a14c8378a2374eb5aeb636e8c4ca7849e48792f5588ad73b3d79ce55afb95660b5052d349082e88b29653d3df674e0fd328f75ad0edb5b63a434c0b7ce44b5c338a0676d6d6648b12e8e7f9570cf24b3ef7b5dfe647f2f36a62114ae5ef23b0a3f6ad15cabba99bf945243553a5329457337884d5671141f6cc438302813801fe2527ad29c1c4e76aedacded581bea2a1b6158b7d8ec3b09e2e6c9a87495f9e6d33188bdc5074d10564d30494e1b1f6af58ab96d11dd4817a0fc17619853792c8785; 
path=/; 
domain=.mysite.com; 
HttpOnly; 
Set-Cookie: SY_REMOTEACCESS=;
Content-Type: text/html Expires: Tue, 01 Jan 1980 04:00:00 GMT X-RE-Ref: 0 -77259901 X-Cache: MISS from 192.168.1.1 X-Cache-Lookup: MISS from 192.168.1.1 Via: 1.0 192.168.1.1 (squid) Connection: close";
";

现在我想将所有的cookie存储到一个数组中

$cookie[1] = "EUID=face313a-dddd-11e0-a694-00000aab0f6c";
$cookie[2] = "MIAMISESSION=face28e8-dddd-11e0-a694-00000aab0f6c:3493353428";
...

我该如何做到这一点?这是正则表达式的工作还是有一个类或函数可以使这更容易?


2
请不要提供cUrl解决方案,那并不符合我的需求。 - John
4个回答

12

另一种方法:

    $headerCookies = explode('; ', getallheaders()['Cookie']);

    $cookies = array();

    foreach($headerCookies as $itm) {
        list($key, $val) = explode('=', $itm, 2);
        $cookies[$key] = $val;
    }
    
    print_r($cookies); // assoc array of all cookies

2
我使用了以下代码:
$domain = "www.google.com";

// open the socket
$f = @stream_socket_client($domain . ":80", $err, $errstr, 30, STREAM_CLIENT_CONNECT);

if(!$f)
  die("Error: " . $err . $errstr);

// prepare the HTTP request headers
$request_headers  = "GET / HTTP/1.1" . "\r\n";
$request_headers .= "Host: " . $domain . "\r\n";
$request_headers .= "Connection: close" . "\r\n";
$request_headers .= "\r\n";

// send the HTTP request
fputs ($f, $request_headers);

// read everything from the socket
$buf = "";
while (!feof($f)) {
  $buf .= fgets ($f, 8192);
}

// close the socket
fclose($f);

// split the headers and the body
$response = preg_split("|(?:\r?\n){2}|m", $buf, 2);
if(!isset($response[1]))
  $response[1] = "";

$headers = array();
$content = $response[1];

$out = preg_split("|(?:\r?\n){1}|m", $response[0]);
foreach($out as $line){
  @list($key, $val) = explode(": ", $line, 2);
  if ($val != null) {
    if(!array_key_exists(strtolower($key), $headers))
      $headers[strtolower($key)] = strtolower(trim($val));
  } else 
    $headers[] = $key;
}

// now you have the response headers in an array
print_r($headers);

接下来要做的是获取存储为字符串值的每个cookie并解析它。为了实现这一点,我使用了一个从Zend Framework中复制的函数fromString(): http://www.sourcexref.com/xref/moodle/nav.html?lib/zend/Zend/Http/Cookie.php.source.html#l274
祝你好运。
附注:此行代码$headers[strtolower($key)] = strtolower(trim($val));将覆盖服务器发送两次的头参数的值。
即:
...
Connection: close
Expires: -1
Connection: close
Cache-control: private, max-age=0
...

2
使用http_parse_headers()函数。

4
要使用**http_parse_headers()**函数,您必须安装PECL扩展... - Cristian Ciocău

1
另一种简短的方式:

if(preg_match_all('/Set-Cookie:[\s]([^;]+)/', $header, $matches)){
    $cookies = $matches[1];
}

它会给你完全想要的东西。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接