2011-09-13 70 views
2

我有一个包含头响应的字符串变量。这样PHP从头字符串获取Cookie

这串由通过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

回答

2

使用http_parse_headers()功能

+3

嗯...要使用** http_parse_headers()**函数,您必须具有PECL扩展名... –

1

我用下面的代码:

$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和解析it.To实现这一点,我使用的功能,我从Zend Framework fromString()http://www.sourcexref.com/xref/moodle/nav.html?lib/zend/Zend/Http/Cookie.php.source.html#l274复制。

祝你好运。

P.S.此行$ headers [strtolower($ key)] = strtolower(trim($ val));如果此命令由服务器发送两次,则将覆盖标头参数的值。

即:

... 
Connection: close 
Expires: -1 
Connection: close 
Cache-control: private, max-age=0 
... 
0

另一个短的路:

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

它会给你你想要的到底是什么

+0

这不是从Cookie读取域和路径。 –

2

另一种方式来做到这一点:

$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