从 curl 响应中提取会话 ID


session id extraction from a curl response

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

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 来存储它。我尝试了一些链接来解决它,但没有任何效果。请帮忙!!

这应该会有所帮助

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

为了正确解决此问题,您应该正确解析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];
}