0

几乎一整天我都在努力解决这个问题。

$result = curl_exec($ch);用来读取响应,我想获得如下所示的 Set-cookie 标头:

Set-Cookie: JSESSIONID=05a0c785d8e2f4856a3f844e3694; Path=/qos; HttpOnly rememberMe=deleteMe; Path=/qos; Max-Age=0; Expires=Thu, 06-Sep-2012 10:50:20 GMT rememberMe=x+8X9hDZEUwM23osStzBmHs6FV/4VJAy18j4gjswG0H9wYKtOsSLu4p4XQ2KjLReNN4V2NokyjT66QkWzYetsde /Gbvmb9iJgV3VsuCvRgFpEFsduig5wHwu0YXjVgQWheiK87+5CNQE1X06z8mbPKu2GZUE/vwUUraR66ZAfxPdRKM24NKJ7HV4UIAPspudvCTJzFRqfkwb+pw cqnslyB6Rb/LB+pqHx2oshnINTwNd1E25+Zjs8qGQFQnHRvI4mRGF+BHC1JPGBV2GD9F7K29+szUlAGJhKr7FtOV1ELhN4JMqP4yuGDH0Dt9naLbRr6oFxxB 9t1+N5gHKVOE0vL5VM54Is5nAnWmhUrx9r3+R99We5phw7XuE1svlcOq2XrVeRJthMy3Xfp70cobUBdN/bOh5/ih9/wg3XwpagsGUKf1raP9mGO4ShDd+NFg8Z+eC+80Yt2wkS+7ZH/UGNh73LkdtCv6WlgFqiB/po20Lu+gCbDEceKt+lTNlU2c1; Path=/qos; Max-Age=15724800; Expires=Fri, 08-Mar-2013 10:50:20 GMT; HttpOnly

我想用';'分开它们 字符。

编辑:

下面的解决方案并没有像我想象的那样工作。也许是因为我的结果中有其他标题:

Status Code: 200 OK
Content-Length: 0
Date: Fri, 07 Sep 2012 10:50:20 GMT
Server: GlassFish Server Open Source Edition 3.1.1
Set-Cookie: JSESSIONID=05a0c785d8e2f4856a3f844e3694; Path=/qos; HttpOnly rememberMe=deleteMe; Path=/qos; Max-Age=0; Expires=Thu, 06-Sep-2012 10:50:20 GMT rememberMe=x+8X9hDZEUwM23osStzBmHs6FV/4VJAy18j4gjswG0H9wYKtOsSLu4p4XQ2KjLReNN4V2NokyjT66QkWzYetsde /Gbvmb9iJgV3VsuCvRgFpEFsduig5wHwu0YXjVgQWheiK87+5CNQE1X06z8mbPKu2GZUE/vwUUraR66ZAfxPdRKM24NKJ7HV4UIAPspudvCTJzFRqfkwb+pw cqnslyB6Rb/LB+pqHx2oshnINTwNd1E25+Zjs8qGQFQnHRvI4mRGF+BHC1JPGBV2GD9F7K29+szUlAGJhKr7FtOV1ELhN4JMqP4yuGDH0Dt9naLbRr6oFxxB 9t1+N5gHKVOE0vL5VM54Is5nAnWmhUrx9r3+R99We5phw7XuE1svlcOq2XrVeRJthMy3Xfp70cobUBdN/bOh5/ih9/wg3XwpagsGUKf1raP9mGO4ShDd+NFg8Z+eC+80Yt2wkS+7ZH/UGNh73LkdtCv6WlgFqiB/po20Lu+gCbDEceKt+lTNlU2c1; Path=/qos; Max-Age=15724800; Expires=Fri, 08-Mar-2013 10:50:20 GMT; HttpOnly

我只需要 Set-cookie

4

3 回答 3

1

使用explode()来分割我们的字符串,像这样:

$dataArray = explode('; ', $result);

结果 $dataArray 是包含所有部分的数组。

于 2012-09-07T12:19:37.087 回答
1

使用此行获取curl_exec()结果的所有元素的数组:

$parsedResult = explode('; ', $result);

请注意,您可以curl_exec()使用 '; 拆分结果。'。

然后,您可以Set-Cookie通过执行以下操作访问该值:

$setCookieValue = substr("Set-Cookie: ", "", $parsedResult[0]);

实质上,您获取 $parsedResult 数组的第一个元素并从中删除“Set-Cookie:”。

于 2012-09-07T12:24:19.673 回答
0

最后我找到了那个灵魂。一次爆炸还不够。

$parsedHeaders = http_parse_headers($result);
$set_cookie_header = $parsedHeaders['Set-Cookie'];
$cookie = $set_cookie_header[0];
$cookie = $cookie[0];
$cookie = explode('; ', $cookie);
$cookie = $cookie[0];
$cookie = substr($cookie, 11); 

我从 php.net 示例中获取的 http_parse_headers 看起来像这样:

function http_parse_headers( $header )
{
        $retVal = array();
        $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach( $fields as $field ) {
            if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                if( isset($retVal[$match[1]]) ) {
                    $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                } else {
                    $retVal[$match[1]] = trim($match[2]);
                }
            }
        }
        return $retVal;
}

我希望它将来对某人有所帮助。

于 2012-09-12T16:52:16.910 回答