CandyCoated
CandyCoated

Reputation: 23

Guzzle - Command & Services: Basic HTTP Auth

I have previously successfully used the guzzlehttp/guzzle v.6.* package with authentication parameters like so:

        $client = new GuzzleClient([
            'base_uri'  => $base_uri ,
            'auth'      => [ $username, $password ]
        ]);

This works great. However, I am now trying to use the "guzzlehttp/guzzle-services": "0.5.*" package to make working with API endpoints easier.

Using the following example from guzzle-services's Github page:

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;

$client = new Client();
$description = new Description([
    'baseUrl' => 'http://httpbin.org/',
    'operations' => [
        'testing' => [
            'httpMethod' => 'GET',
            'uri' => '/get/{foo}',
            'responseModel' => 'getResponse',
            'parameters' => [
                'foo' => [
                    'type' => 'string',
                    'location' => 'uri'
                ],
                'bar' => [
                    'type' => 'string',
                    'location' => 'query'
                ]
            ]
        ]
    ],
    'models' => [
        'getResponse' => [
            'type' => 'object',
            'additionalProperties' => [
                'location' => 'json'
            ]
        ]
    ]
]);

$guzzleClient = new GuzzleClient($client, $description);
$result = $guzzleClient->testing(['foo' => 'bar']);

How and where in the world do I add auth parameters when using the "guzzlehttp/guzzle-services": "0.5.*" package?

I have tried about every way possible but cannot get it to work.

Upvotes: 2

Views: 988

Answers (2)

sanzante
sanzante

Reputation: 914

I've succesfully managed to use Guzzle 6.2.2 and Guzzle Services 1.0.0 with Basic Auth with this code:

$config['auth'] = array('user', 'pass');
$client = new Client($config);

You may need other settings, of course, but for Basic Auth only this is needed. Check the GuzzleHttp\Client::applyOptions class method to see when Guzzle uses this setting.

It's very similar to @revo answer but without the main 'defaults' array.

Those are my guzzle installed packages:

"
gimler/guzzle-description-loader     v0.0.2  Load guzzle service description from various file formats
guzzlehttp/command                   1.0.0   Provides the foundation for building command-based web service clients
guzzlehttp/guzzle                    6.2.2   Guzzle is a PHP HTTP client library
guzzlehttp/guzzle-services           1.0.0   Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, se...
guzzlehttp/promises                  1.3.0   Guzzle promises library
guzzlehttp/psr7                      1.3.1   PSR-7 message implementation
"

Upvotes: 1

revo
revo

Reputation: 48711

I doubt if Description class provides a way to merge authentication information to the request. But you can add them while instantiating a new Client in Guzzle v5.x like this:

$client = new Client(['defaults' => ['auth' => ['user', 'pass']]]);

Upvotes: 0

Related Questions