I'm trying to make an API that use digest as authentication, When I access API via curl command line using this command, it's work
curl --digest --user website:website http://localhost/api/test/users
but when running api client using Guzzle 6 php library using this code
$handler = new GuzzleHttp\Handler\CurlHandler();
$stack = GuzzleHttp\HandlerStack::create($handler); // Wrap w/ middleware
$client = new GuzzleHttp\Client(['base_uri' => 'http://localhost', 'handler' => $stack]);
try {
$request = new GuzzleHttp\Psr7\Request('GET', $req_uri, [
'auth' => ['website', 'website', 'digest']
]);
$response = $client->send($request, ['timeout' => 2]);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
return $response;
It's not working, it says
401 Unauthorized` response: {"status":false,"error":"Unauthorized"}
how to fix this? and how to implement custom handler guzzle php in the right way ?
I had the same issue and I noticed php-curl extension wasn't installed at all. After installing it digest auth worked. To install the extension type:
sudo apt-get install php-curl
I had the same problem, figured out that it may depend on the version of your curl php extension. I didn't have the possibility to upgrade it, so I had to handle digest auth myself (basically the info in wikipedia is enough https://en.wikipedia.org/wiki/Digest_access_authentication).
You send the first request and get a response with code 401 and "WWW-Authenticate" header (it contains some values you'll need to generate your hashes), then just generate "Authorization" header and use it in your guzzle requests.
Sorry I can't provide a code example at the moment, maybe later, if you don't make it yourself :)