几乎整整一天我都在努力解决这个问题。
我正在使用$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
我想用';'分开它们炭。
编辑:
下面的解决方案并不像我想象的那样有效。也许是因为我的resoult中有其他标题:
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
答案 0 :(得分:1)
答案 1 :(得分:1)
使用此行获取curl_exec()
结果的所有元素的数组:
$parsedResult = explode('; ', $result);
请注意,您可以将curl_exec()
结果拆分为'; ”。
然后,您可以通过以下方式访问Set-Cookie
值:
$setCookieValue = substr("Set-Cookie: ", "", $parsedResult[0]);
本质上,你接受$ parsedResult数组的第一个元素并从中删除“Set-Cookie:”。
答案 2 :(得分: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;
}
我希望将来能帮到某人。