我正在编写一个脚本,通过套接字与SMTP服务器对话,并尝试实现DIGEST-MD5身份验证,但在解析AUTH命令后发回的字符串时遇到了问题。
在base64_decode()之后,它看起来是这样的:
realm="smtp.domain.net",nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=",qop="auth,auth-int",charset=utf-8,algorithm=md5-sess我想使用str_getcsv(),但服务器仍然在PHP5.2上,所以我得到了以下代码from the comments on PHP.net,它看起来很好:
<?php
if (!function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter=',', $enclosure='"', $escape=null, $eol=null) {
$temp=fopen("php://memory", "rw");
fwrite($temp, $input);
fseek($temp, 0);
$r = array();
while (($data = fgetcsv($temp, 4096, $delimiter, $enclosure)) !== false) {
$r[] = $data;
}
fclose($temp);
return $r;
}
}但它返回以下内容:
array (
0 =>
array (
0 => 'realm="smtp.domain.net"',
1 => 'nonce="2PuESkmrNzGu/5b8N6eIYQoW7mSlScnYAB/PSYebkYo="',
2 => 'qop="auth',
3 => 'auth-int"',
4 => 'charset=utf-8',
5 => 'algorithm=md5-sess',
),
)请注意,索引2和3应该是单个qop="auth,auth-int"。
在写这篇文章的时候,我意识到也许fgetcsv()希望$enclosure字符包含整个字段,而不只是其中的一部分,但在这种情况下,我必须知道如何正确地解析这个字符串。
发布于 2013-01-26 06:28:07
在我用谷歌搜索'PHP DIGEST-MD5‘时,我遇到了a patch for another project,它用下面的行处理相同的格式字符串:
preg_match_all('/(\w+)=(?:"([^"]*)|([^,]*))/', $challenge, $matches);这给了我:
array (
0 =>
array (
0 => 'realm="smtp.domain.net',
1 => 'nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=',
2 => 'qop="auth,auth-int',
3 => 'charset=utf-8',
4 => 'algorithm=md5-sess',
),
1 =>
array (
0 => 'realm',
1 => 'nonce',
2 => 'qop',
3 => 'charset',
4 => 'algorithm',
),
2 =>
array (
0 => 'smtp.domain.net',
1 => 'AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=',
2 => 'auth,auth-int',
3 => '',
4 => '',
),
3 =>
array (
0 => '',
1 => '',
2 => '',
3 => 'utf-8',
4 => 'md5-sess',
),
)然后我可以用这个循环填充一个有用的数组:
$authvars = array();
foreach( $auth_matches[1] as $key => $val ) {
if( !empty($auth_matches[2][$key]) ) {
$authvars[$val] = $auth_matches[2][$key];
} else {
$authvars[$val] = $auth_matches[3][$key];
}
}这给了我:
array (
'realm' => 'ns103.zabco.net',
'nonce' => 'xITX1qgqlCDmYX6IrctN0WZRx7+Q4W7jjaXCCeUZnU8=',
'qop' => 'auth,auth-int',
'charset' => 'utf-8',
'algorithm' => 'md5-sess',
)它不是很漂亮,但它可以完成工作。
发布于 2013-01-26 06:54:35
$decodedString = 'realm="smtp.domain.net",nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=",qop="auth,auth-int",charset=utf-8,algorithm=md5-sess';
parse_str(preg_replace('/(?:(")(.*?)("))?,(?:(")(.*?)("))?/','$1$2$3&$4$5$6',$decodedString), $values);
var_dump($values);如果还想去掉结果数组值两边的引号,请使用
$values = array_map(
function ($value) {
return trim($value,'"');
},
$values
);
var_dump($values);https://stackoverflow.com/questions/14530989
复制相似问题