如何从cURL头输出中提取PHPSESSID

时间:2011-12-14 19:14:11

标签: php session cookies curl

我有一个问题:

如果我将cURL设置为在输出中包含标题信息,如何从该输出中仅提取PHPSESSID信息?

输出看起来像这样:

OUTPUT: HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 26 Sep 2011 19:10:48 GMT    Server: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o X-Powered-By: PHP/5.3.5 Set-Cookie: PHPSESSID=lsa18d8r126ps1vfp4h05dh8r4; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache     Transfer-Encoding: chunked Content-Type: text/html 

3 个答案:

答案 0 :(得分:2)

这是一个非常简单的解决方案,我不知道是否有任何卷曲方式来检索它,但它可以用简单的正则表达式处理。

$foo="HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 26 Sep 2011 19:10:48 GMT    Server: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o X-Powered-By: PHP/5.3.5 Set-Cookie: PHPSESSID=lsa18d8r126ps1vfp4h05dh8r4; path=/ Expires: Thu,19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache     Transfer-Encoding: chunked Content-Type: text/html";

echo preg_replace("/.*PHPSESSID=([^;]*);.*/", "\\1", $foo);

如果字符串在语法上对于每个请求都相同,则可以如图所示进行。 “;”这标志着会话id的结束是强制性的,否则regexp会失败并返回整个字符串,因为它不匹配。也许你应该在提取前使用preg_match来确保它在语法上是正确的,甚至在字符串中都存在PHPSESSID

答案 1 :(得分:0)

您可以编写更多代码来解析响应中的标头。不幸的是,curl并不适合你。

以下是如何执行此操作的快速示例:

$url = 'http://stackoverflow.com';

// prepare curl
    $curl_handle = curl_init();
    curl_setopt( $curl_handle, CURLOPT_HEADER, true );
    curl_setopt( $curl_handle, CURLOPT_URL, $url );

// make request
    ob_start();
        $result = curl_exec ( $curl_handle ); // execute the curl command
    $response = ob_get_clean();

// parse the response
    $info = curl_getinfo( $curl_handle );
    curl_close( $curl_handle );
    $raw_headers = explode("\n", substr($response, 0, $info['header_size']) );

// Parse (named) headers
    $headers = array();

    foreach( $raw_headers as $header ){

        if( preg_match('/^(.*?)\\:\\s+(.*?)$/m', $header, $header_parts) ){
            $headers[$header_parts[1]] = $header_parts[2];
        }
    }

// get the body
    $body = substr($response, -$info['download_content_length']);  


// now you can get your headers from $headers by index
    echo $headers['PHPSESSID'];

$ headers数组的var_dump如下所示:

array(7) {
  ["Cache-Control"]=>
  string(19) "public, max-age=57
"
  ["Content-Type"]=>
  string(25) "text/html; charset=utf-8
"
  ["Expires"]=>
  string(30) "Wed, 14 Dec 2011 23:37:21 GMT
"
  ["Last-Modified"]=>
  string(30) "Wed, 14 Dec 2011 23:36:21 GMT
"
  ["Vary"]=>
  string(2) "*
"
  ["Date"]=>
  string(30) "Wed, 14 Dec 2011 23:36:23 GMT
"
  ["Content-Length"]=>
  string(7) "197298
"
}

答案 2 :(得分:0)

我只想改善@ evildead的答案。如果标题输出字符串包含换行符,或者在sessid之后cookie标题结束时,他的答案不起作用(所以没有分号)。

$foo="HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 26 Sep 2011 19:10:48 GMT\r\nServer: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o X-Powered-By: PHP/5.3.5 Set-Cookie: PHPSESSID=lsa18d8r126ps1vfp4h05dh8r4; path=/ Expires: Thu,19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache     Transfer-Encoding: chunked Content-Type: text/html";
preg_match("/PHPSESSID=(.*?)(?:;|\r\n)/", $foo, $matches);
echo $matches[1];

如果查看PHPSESSID的存在也是个好主意:

$foo="HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 26 Sep 2011 19:10:48 GMT\r\nServer: Apache/2.2.16 (Win32) mod_ssl/2.2.16 OpenSSL/0.9.8o X-Powered-By: PHP/5.3.5 Set-Cookie: PHPSESSID=lsa18d8r126ps1vfp4h05dh8r4; path=/ Expires: Thu,19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache     Transfer-Encoding: chunked Content-Type: text/html";
if(preg_match("/PHPSESSID=(.*?)(?:;|\r\n)/", $foo, $matches)){
    echo $matches[1];
}else{
    /* Do something */
}