2012-09-07 115 views
0

幾乎整天我都在爲這個問題而苦苦掙扎。解析curl_exec響應

我使用$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 

而我只需要設置Cookie

回答

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; 
} 

我希望它能夠幫助別人的未來。

1

使用explode()分裂我們的字符串,像這樣:

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

,並導致$ dataArray中是所有部件的陣列。

1

使用此行得到curl_exec()結果的所有元素的數組:您可以分割curl_exec()結果與「

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

注意; 」。

然後,您可以這樣做訪問Set-Cookie值:

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

從本質上說,你把$ parsedResult數組的第一個元素和「設置Cookie:」刪除它。

+0

感謝您的幫助,我解決了它併發布了我的答案:) – dargod