2011-12-23 8 views
0

次の応答がサーバーから送信されています。 $ realm、$ nonce、$ opaqueという変数にどのように文字列www-authenticateの部分を抽出するのですか?変数でhttpヘッダーの応答値を取る方法は?

次の出力は、カール要求によって生成されていると私はレスポンスヘッダを印刷しています:

HTTP/1.1 401 Unauthorized 
Cache-Control: private 
Date: Fri, 23 Dec 2011 05:49:41 GMT 
Content-Length: 0 
Content-Type: text/html 
WWW-Authenticate: Digest realm="[email protected]", nonce="3133323436313933383137343820335916269c13227f30b07bd83a1c7e0d", opaque="6e6f742075736564" 
RETS-Version: RETS/1.5 
X-Powered-By: Servlet/2.5 JSP/2.1 

答えて

1

まず、一般的な配列にヘッダーを解析:

$headers = explode("\n", $headers); 
foreach ($headers as $header) { 
    list($key, $value) = explode(': ', $header, 2); 
    $headers[$key] = $value; 
} 

を次に、解析WWW-Authenticateヘッダーは次のようになります。

$params = array(); 
preg_match_all('/(\w+)="([^"]+)"/', $headers['WWW-Authenticate'], $matches, PREG_SET_ORDER); 
foreach ($matches as $match) { 
    $params[$match[1]] = $match[2]; 
} 
+0

この短いコードは、複数行のヘッダー値に問題があります。 RFCはここにあります:[rfc2616 4.2 Message Headers](http://tools.ietf.org/html/rfc2616#section-4.2) – hakre

0

彼は関数get_headers http://es2.php.net/get_headersあなたはヘッダーの値で配列を得ることができます。

+0

これは、現在要求されているスクリプトではなく、指定されたURLのヘッダーを取得します – kontur

0
$headers = getallheaders(); 
preg_match_all('/\"([^"])*\"/', $headers['WWW-Authenticate'], $matches); 
print_r($matches); 

これは、getallheaders()とヘッダを取得し、あなたのWWW-AUTHをピックアップして、$への引用符の間のすべての値は、フィルタ正規表現によって配列に一致します。 $ matches [0]などで値にアクセスしてください。

関連する問題