Anonymonk:
More of your code would be quite helpful. I think the most likely answer is that
$user and
$passwd don't contain what you think they do, and the 401 is being generated because the user name and password do not match authenticated values. Just a guess without a more substantial code snippet.