Guzzle HTTP - add Authorization header directly into request

I'm using Guzzle 6. If you want to use the Basic Auth Scheme:

$client = new Client();
$credentials = base64_encode('username:password');
$response = $client->get('url',
        [
            'headers' => [
                'Authorization' => 'Basic ' . $credentials,
            ],
        ]);

I don't know how I missed reading that you were looking for the Basic auth header, but nonetheless hope this helps somewhat. If you are just looking to add the Authorization header, that should be pretty easy.

// Set various headers on a request
$client->request('GET', '/get', [
'headers' => [
    'Authorization'     => 'PUT WHATEVER YOU WANT HERE'
    ]
]);

I build up my request in Guzzle piece by piece so I use the following:

$client = new GuzzleHttp\Client();
$request = $client->createRequest('GET', '/get');
$request->addHeader('X-Authorization', 'OAuth realm=<OAUTH STUFF HERE>');
$resp = $client->send($request);

Hope that helps. Also, make sure to include the version of Libraries you are using in the future as syntax changes depending on your version.


From the looks of things, you are attempting to use an API key. To obtain your desired effect, simply pass null in as the username, like below.

$client->request(
    $method,
    $url,
    [
        'auth' => [
            null,
            $api_key
        ],
    ]
);