会话ID从卷曲响应中提取

时间:2014-11-13 05:42:57

标签: php session curl

我得到了以下格式的卷曲响应。

HTTP/1.1 200 OK Date: Thu, 13 Nov 2014 05:34:03 GMT Server: Apache/2.4.9 (Ubuntu) Set-Cookie: sid=o58lq30rgnqsep720kbje8cap5; path=/ Cache-Control: no-cache Allow: POST Transfer-Encoding: chunked Content-Type: application/json; charset=UTF-8 {"Response":{"reasonCode":"0","reasonText":"The consumer was successfully logged in with a session cookie","reasonImageURL":"","satn":"213456","rrn":"1415856843"}}

我想获取会话ID来存储它。我尝试了一些链接来解决它,但没有任何效果。请帮忙!!

2 个答案:

答案 0 :(得分:0)

这应该有帮助

preg_match('/SID=(\w+)/', $response['body'], $matches);
$sid = $matches[1]; //sid

答案 1 :(得分:0)

为了正确解决这个问题,您应该正确解析HTTP响应...

$response = "";
list($headers, $body) = explode("\r\n\r\n", $response, 2);
$raw_headers = explode("\r\n", $headers);
array_shift($raw_headers); // exclude first row 'HTTP/1.1 200 OK'
$headers = array();
foreach($raw_headers as $header) {
  list($key, $value) = explode(": ", $header, 2);
  $headers[strtoupper($key)] = $value;
}
$cookie = $headers['SET-COOKIE'];
preg_match('/sid=([a-f\d])/i', $cookie, $matches);
if ($matches) {
  $session_id = $matches[1];
}
相关问题