Search code examples
phpjsonauthenticationguzzledigest

Digest authentication with Guzzle


Trying to get to get JSON response from API that uses digest authentication. I am using Guzzle for the client.

This is what I have so far and doesn't seem to work. Any suggestion?

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client([
'base_uri' => 'https://10.1.1.1',
'timeout'  => 2.0,
]);

$client->setDefaultOption('verify', false);
$client->request('POST', '/json', ['auth' => ['username', 'password', 'digest']]);

Solution

  • <?php
    
    require 'vendor/autoload.php';
    use GuzzleHttp\Client;
    
    $client = new Client();
    
    $query = '{"id":1}'; //json payload if any
    
    $result = $client->request(
            'POST',
            'https://10.1.1.1/json', [
                'verify' => false,
                'auth' => ['username', 'password', 'digest'],
                'json' => json_decode($query, true),
            ]);