0

I have the following response being sent from a server. How do i extract the part of the string www-authenticate into variable called $realm, $nonce, $opaque ?

The following output is being generated by a curl request and i'm printing the response headers :

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

3 Answers 3

1

First, parse the headers into a general array:

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

Then, parse the WWW-Authenticate header with something like this:

$params = array();
preg_match_all('/(\w+)="([^"]+)"/', $headers['WWW-Authenticate'], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
    $params[$match[1]] = $match[2];
}
Sign up to request clarification or add additional context in comments.

1 Comment

This short code has problems with multi-line header values. Just noting, RFC is here: rfc2616 4.2 Message Headers
0

Using the function get_headers http://es2.php.net/get_headers you can get an array with the values of the header.

1 Comment

this will get the headers for a given url, not for the currently requested script
0
$headers = getallheaders();
preg_match_all('/\"([^"])*\"/', $headers['WWW-Authenticate'], $matches);
print_r($matches);

This gets the headers with getallheaders(), picks your www-auth, and then filters every value between quotes into the $matches array by a regular expression. Access your values via $matches[0] etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.