Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do HTTP basic authentication using Guzzle?

I want to do basic access authentication using Guzzle and I am very new to programming. I have no clue what to do. I tried to do this using curl but my environment requires using guzzle.

like image 342
Gopi K Mishra Avatar asked Jun 22 '15 00:06

Gopi K Mishra


People also ask

How do I set up HTTP basic authentication?

For HTTP basic authentication, each request must include an authentication header, with a base-64 encoded value. Where siteName is the company name you use to log in to Eloqua, and username and password are your Eloqua username and password.

How do I set Basic Authentication in HTTP header?

Basic Auth:The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send. Note: Base64 encoding does not mean encryption or hashing!


1 Answers

If you're using Guzzle 5.0 or newer, the docs say that basic auth is specified using the auth parameter:

$client = new GuzzleHttp\Client(); $response = $client->get('http://www.server.com/endpoint', [     'auth' => [         'username',          'password'     ] ]); 

Please note that the syntax is different if you're using Guzzle 3.0 or earlier. The constructor is different, and you also need to explicitly use the send method on a request to get a response:

$client = new Guzzle\Http\Client(); $request = $client->get('http://www.server.com/endpoint'); $request->setAuth('username', 'password'); $response = $request->send(); 
like image 98
ffflabs Avatar answered Sep 22 '22 02:09

ffflabs